NOOB coding help....

bruno modulix onurb at xiludom.gro
Tue Feb 22 07:49:28 EST 2005


Igorati wrote:
> #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.)'

so 0 is not a valid value ?
Since you only want numbers, any alpha char could be used to exit...

> def getint():
>      return int(raw_input('Number? '))

What happens if the user type "foo" ?

> 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'))

Take care of removing hard-coded filenames... (this is ok for testing 
but should not be released like this)

> 
> 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:

First, raw_input() is a function, you need to use the () operator to 
*call* (execute) the function.

Then, raw_input() returns a string. A string is something between 
quotes. Yes is a symbol (a variable name, function name or like), not a 
string. And since this symbol is not defined, you have an exception.

What you want to do is to compare the string returned by the raw_input() 
function to the literral string "Yes". What you're doing in fact is 
comparing two symbols, one being defined, the other one being undefined...

   if raw_input() == "Yes":

> 
>     f = open("avg.py","r")
Same remark as above about hard-coded file names...

>     avg.py = f.read()

Notice that the dot is an operator. Here you trying to access the 
attribute 'py' of an (actually inexistant) object named 'avg'. What you 
want is to bind the data returned by f.read() to a variable.

       data = f.read()


>     print 'The Mean average is:', mean
>     print 'The Median is:', meadian
>     print 'The Mode is:', mode

At this time, mean, meadian and mode are undefined symbols. Since you 
used the pickle module to persist your data, you should use it to 
unserialize'em too. The pickle module is well documented, so you may 
want to read the manual.

> 
> 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

Can't tell you since I'm running python 2.3.x here (sorted() is new in 2.4)

> 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

See above.

> I cannot understand how I can define yes so that when they type yes the
> data is printed out. 

Yes = 'yes' ?-)

> Also if they type no how do I make the program do
> nothing. Or is that just infered.

May I suggest that you :
- subscribe to the 'tutor' mailing-list (it's a ml for absolute beginners)
- do some tutorials (there's one with the doc, and many others freely 
available on the net)
- use the interactive python shell to test bits of your code ?

HTH
Bruno
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for 
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list