NOOB coding help....

Brian van den Broek bvande at po-box.mcgill.ca
Tue Feb 22 05:04:36 EST 2005


Igorati said unto the world upon 2005-02-22 03:51:
> #This program will ask for a user to imput numbers. The numbers will then
> be calculated
> #to find the statistical mean, mode, and median. Finallly the user will be
> asked
> #if he would like to print out the answers.
> numbers = [ ] 
> print 'Enter numbers to add to the list. (Enter 0 to quit.)'
> def getint():
>      return int(raw_input('Number? '))
> 
> numbers = sorted(iter(getint, 0))
> 
> numbers
> [2, 3, 5, 7]
> def variable_median(x):
>      
>      return sorted(x)[len(x)//2]
> def variable_mode(x):
>      
>      counts = {}
>      for item in x:
>          counts[x] = counts.get(x, 0) + 1
>      
>      return sorted(counts, key=counts.__getitem__, reverse=True)[0]
> 
> 
> 
> s = variableMean(numbers)
> y = variableMedian(numbers)
> t = variableMode (numbers)
> 
> import pickle
> pickle.dump((s, y, t), file('avg.pickle', 'w'))
> 
> 
> print 'Thank you! Would you like to see the results after calculating'
> print 'The mode, median, and mean? (Please enter Yes or No)'
> print 'Please enter Yes or No:'
> 
> 
> if raw_input == yes:
> 
>     f = open("avg.py","r")
>     avg.py = f.read()
>     print 'The Mean average is:', mean
>     print 'The Median is:', meadian
>     print 'The Mode is:', mode
> 
> 
> I got the error:
> 
> Traceback (most recent call last):
>   File "C:\Python24\Lib\New Folder\Wade_StoddardSLP3-2.py", line 9, in ?
>     numbers = sorted(iter(getint, 0))
>   File "C:\Python24\Lib\New Folder\Wade_StoddardSLP3-2.py", line 7, in
> getint
>     return int(raw_input('Number? '))
> TypeError: 'str' object is not callable
> 
> and this one for my if statement:
> 
> Traceback (most recent call last):
>   File "C:\Python24\Lib\New Folder\Wade_StoddardSLP3-2.py", line 39, in ?
>     if raw_input == Yes:
> NameError: name 'Yes' is not defined
> 
> I cannot understand how I can define yes so that when they type yes the
> data is printed out. Also if they type no how do I make the program do
> nothing. Or is that just infered.

Hi,

have you tried examining the objects you are trying to put together to 
see why they might not fit?

For instance, look what happens when you run this, entering 42 at the 
prompt:

ri = raw_input('Gimme!    ')
print type(ri)
print type(int(ri))
print ri == 42
print ri == '42'
name_not_defined_in_this_code

Does looking at the output of that help?

Best,

Brian vdB




More information about the Python-list mailing list