does python find errors where there are none? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
while plotting functions with heaviside function I came up with this piece of code, in Idle:
import numpy as np
import matplotlib.pyplot as plt
n_i = [-5, 5]
n = np.linspace(n_i[0], n_i[1], 1E3)
u1 [n+30>=0] = 1
u2 [n-15>=0] =1
u3 = u1 - u2
x = np.sin(2*np.pi*(n/15))*u3
plt.axis([-30,15,-5,5])
plt.xlabel('$n$',fontsize=20)
plt.ylabel('$x(n)$',fontsize=20)
plt.stem(n, x, "--k", linefmt='black', basefmt='black')
plt.grid()
plt.show()
and prior to today, it ran without errors, same with all other of my plots, I've been dealing with python for two years now and throughout classes it had the habit of finding errors where even teachers don't see them. am I missing something here? it says "u1 is not defined", but it is. I even compared with coworkers and classmates alike, haven't seen it put in any other way in the code for the plot. help!

You try to assign a value to u1[...] without even having created u1.
You should init u1 before trying to init its elements.

Related

I can get this to run without def total_numbers(number_set): but not with it. Why? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I would like to create a function using a for loop that totals up a list of numbers. I know there are some inbuilt functions in python for this but would like to use something like below.
I can get this to run without def total_numbers(number_set): but not with it.
Why not?
number_set = [1, 12, 23, 16]
def total_numbers(number_set):
total = 0
for number in number_set:
total += number
print(total)
Thanks :-)
With def you define a method, you create and only create it, you don't use it (call it)
To call it, take its name put parenthesis and fill the parameters
def total_numbers(number_set):
total = 0
for number in number_set:
total += number
print(total)
values = [1, 12, 23, 16]
total_numbers(values)

Numpy producing different random numbers despite seed [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I had a problem with NumPy producing different random numbers despite seeding.
code:
import numpy as np
import random
random.seed(1234)
vec = np.random.normal(0, 1, size=6)
print(vec)
As I had set the seed, I expected vec to contain the same random numbers in every run.
But what I got:
run: vec = [-0.80715758 0.8377004 -0.98711439 -0.23424405 -1.85368722 0.97827818]
run: vec = [-0.74409802 -0.85180205 0.84585489 -0.40506222 -1.31125093 -1.23055255]
run: vec = [ 2.21521007 1.38577035 0.25437804 0.84529466 -0.83334042 1.00452671]
I was just posting this question when I found the solution.
But to prevent others from doing the same mistake, I thought it might be a good idea to post it anyway.
My mistake was the following:
I set the seed of random, not of np.random!
When setting the seed of the NumPy random number generator, everything works as expected:
import numpy as np
np.random.seed(1234)
vec = np.random.normal(0, 1, size=6)
vec will always contain the same random numbers.

'int' object is not subscriptable yet all associated variables are fine [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I seem to have a line of code with an object not subscribable fault but all items in the equate check out individually. The equation that gives me the problem is this
oRetVal['Assay'] = tRS2[oCSNo_dx[aRow[oFields_dx['CycleRef']]][oFields_dx['Assay']]]
tRS2 = a recordset returned from an sqlite database as a tuple
oCSNo_dx = an object used as an index
aRow = an array of values representing a single row from a db table
oFields_dx = another object used as an index
On the lines immediately prior to this error line I run the following tests.
print(oCSNo_dx[aRow[oFields_dx['CycleRef']]]) # get 1
print(oFields_dx['Assay']) # get 21
print(tRS2[1][21]) # get 0.0
print(tRS2[1]) # get single recordset (correct one) as expected
print(tRS2[oCSNo_dx[aRow[oFields_dx['CycleRef']]]][oFields_dx['Assay']]) # get 0.0
oRetVal['Assay']=0.0 # No Fault
oRetVal['Assay']=tRS2[1][21] # No fault
print(oRetVal['Assay']) # get 0.0
From the tests oRetVal['Assay'] should take the value 0.0 from tRS2[1][21] in the full equation but it gives me the error. Just to be sure I ran the following and no error was raised ..
s = oCSNo_dx[aRow[oFields_dx['CycleRef']]]
p = oFields_dx['Assay']
oRetVal['Assay']=tRS2[s][p]
I have gone with this for now but I would dearly like to know why I couldn't do the one liner in case this is a syntax thing and I come across it again. Idea's anyone?
Your square brackets are in the wrong place. You're not looking up tRS2[1][21], but tRS2[1[21]], hence the error. It should be:
tRS2[oCSNo_dx[aRow[oFields_dx['CycleRef']]]][oFields_dx['Assay']]
But really, don't do it like this; it's impossible to debug, as you have found. Set some intermediate variables and use them in the lookup.

(TypeError: 'float' object is not callable) when doing math [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am writing a code to calculate all of the energy levels for an electron as it jumps to different orbital levels in the Bohr model. I have some math written, but when I try to run it it says TypeError: 'float' object is not callable. What do I need to do?
Here is my code:
loopone = True
if (work == Element_symbols[0]):
energy = ((charge[0]*(0-13.6))/1)
print ('This is the energy level for the electron at the N=1 state.')
print (energy)
print ('These are all of the possible energies from the n=6 level')
nrgone = (-13.6((1/(5*5)-(1/(6*6)))))
nrgtwo = (-13.6((1/(4*4)-(1/(6*6)))))
nrgthree = (-13.6((1/(3*3)-(1/(6*6)))))
nrgfour = (-13.6((1/(2*2)-(1/(6*6)))))
nrgfive = (-13.6((1/(1*1)-(1/(6*6)))))
Energies = (nrgone, nrgtwo, nrgthree, nrgfour, nrgfive)
print (Energies)
loopone = False
I believe your problem is here
nrgone = (-13.6((1/(5*5)-(1/(6*6)))))
Don't you mean (notice the multiplication sign)?
nrgone = (-13.6*((1/(5*5)-(1/(6*6)))))

Look up for a value in a csv file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I need to look up a value in a csv file given a criteria in a function. When I run my program I get all the values but not the one associated with my entry. Any help will be appreciated.
The date looks something like this:
rose,7.95
lily,3.95
begonia,5.95
The function I created is:
def problem3_8(csv_pricefile, flower):
import csv
archivo = open(csv_pricefile)
for row in csv.reader(archivo):
if flower in row[0]:
print(row[1])
archivo.close()
When I ran the program using the next line:
problem3_7("flowers.csv","rose")
I get all the value in the file, like this:
7.95
3.95
5.95
But the answer should be just the value associated with the second entry.
7.95
Thanks
I ran your code given and had the correct output of 7.95.
Is it possible you called the wrong function? In your question you referred to the function problem3_7 instead of the function problem3_8

Categories