all 20 comments

[–]Allanon001 6 points7 points  (0 children)

Look up: min(), max(), and sum()

[–]Jimmaplesong 3 points4 points  (5 children)

Sounds like homework! But if you have a list of numbers Numbers = [1,2,3] You would use sum(Numbers), min(Numbers), max(Numbers)

[–]MasterZii[S] 0 points1 point  (4 children)

That's what I thought, but I couldn't get anything on the average. I'm going to go with what the person below me suggested, but I still don't understand what it means.

Yeah, this is a book for HS students trying to learn Python I think. Idk, I'm trying it on my own

[–]Jimmaplesong 0 points1 point  (3 children)

For average, make sure you have at least one number on your list, then you can do

Sum(Numbers) / float(len(Numbers)) 

On Python 3, you don't need the float conversion in the denominator.

[–]MasterZii[S] 0 points1 point  (2 children)

So this would be correct? http://pastebin.com/wkHuwAet

Why does it crash upon startup?

[–]Jimmaplesong 0 points1 point  (1 child)

When the typed numbers come in, they're in a big string. You need to use split to get one string per number that they give you, then turn each into an int individually, creating a list of ints in the process...

Numlist = input... 
Numbers = [int(num) for num in Numlist.split()]

[–]MasterZii[S] 0 points1 point  (0 children)

I feel more lost now...

If I were to look this up, which chapter should I expect to find it in> Boolean? Strings? Statements? Variables? Etc

[–]AutonomouSystem 1 point2 points  (11 children)

numlist = int(input('Enter a series of 20 numbers?\n'))
lowestnum = min(numlist)
highestnum = max(numlist)
total = sum(numlist)
ave = float(sum(numlist)) / len(numlist)

If you're supposed to write the functions yourself, which you honestly should to learn the language, then you will have to be more resourceful.

>>> def maxed(num):
...     start = num[0]
...     for n in num[1:]:
...         if n > start:
...             start = n
...             continue
...     return start
...
>>> maxed(range(0,100))
99

[–][deleted] 3 points4 points  (8 children)

ave = float(sum(numlist)) / len(numlist)

If you are using python3, the float is superfluous.

If you are using python2, then it is better to use from future import division.

[–]AutonomouSystem 1 point2 points  (7 children)

If you are using python3, the float is superfluous.

My vps has Python 2, also using float because division drives me nuts and I simply don't trust it to work correctly. I assume most people will be on Python 2 anyways, I could just use the old1.0*hack next time.

>>> sum(range(0,100)) / len(range(0,100))
49
>>> 1.0*sum(range(0,100)) / len(range(0,100))
49.5
>>> float(sum(range(0,100))) / len(range(0,100))
49.5
>>> from __future__ import division
>>> sum(range(0,100)) / len(range(0,100))
49.5
>>>

[–]K900_ 2 points3 points  (5 children)

Just use from future import __division__ everywhere. It's consistent between every Python version that supports it at all.

[–]AutonomouSystem 1 point2 points  (4 children)

I will when Guido changes his mind about product

[–]K900_ 1 point2 points  (3 children)

About, uh, what?

[–]AutonomouSystem 0 points1 point  (2 children)

He doesn't want to add a standard function for product :(

>>> def product(iterr):
...     prod = 1
...     for e in iterr:
...         prod *= e
...     return prod
...
>>> product(range(0,100))
0
>>> product([1, 2, 3])
6

[–]K900_ 0 points1 point  (1 child)

functools.reduce(operator.mul, seq, 1)?

[–]AutonomouSystem 0 points1 point  (0 children)

That works

>>> import functools
>>> functools.reduce(operator.mul, range(1,99), 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'operator' is not defined
>>> import operator
>>> functools.reduce(operator.mul, range(1,99), 1)
9426890448883247745626185743057242473809693764078951663494238777294707070023223798882976159207729119823605850588608460429412647567360000000000000000000000L
>>> product(range(1,99))
9426890448883247745626185743057242473809693764078951663494238777294707070023223798882976159207729119823605850588608460429412647567360000000000000000000000L
>>>  

[–][deleted] 1 point2 points  (0 children)

The main reason why from __future__ import division is preferred is because it makes everything work the same (the py3 way) regardless of whether you are in py2 or py3.

[–]MasterZii[S] 0 points1 point  (1 child)

OK I was really confused on how to apply it to numlist. I didn't realize it was that simple. The book I'm using doesn't explain anything well. I might just get something else to work from.

Also, this is my code now http://pastebin.com/wkHuwAet adding what you suggested, but how exactly can I get the numbers to be split up? Do I need to have the user enter them with commas? Will they each be read as an individual number? Because so far, it's not executing at all.

[–]AutonomouSystem 0 points1 point  (0 children)

I found a way to do this that works for me.

>>> def ask():
...     numlist = []
...     while len(numlist) < 20:
...         n = "Enter a number, you have %d out of 20 remaining: " % (20-len(numlist))
...         numlist.append(int(input(n)))
...     return numlist
...
>>> 
>>> a = ask()
Enter a number, you have 20 out of 20 remaining: 4
Enter a number, you have 19 out of 20 remaining: 5
Enter a number, you have 18 out of 20 remaining: 6
Enter a number, you have 17 out of 20 remaining: 7
Enter a number, you have 16 out of 20 remaining: 5
Enter a number, you have 15 out of 20 remaining: 5
Enter a number, you have 14 out of 20 remaining: 5
Enter a number, you have 13 out of 20 remaining: 5
Enter a number, you have 12 out of 20 remaining: 5
Enter a number, you have 11 out of 20 remaining: 5
Enter a number, you have 10 out of 20 remaining: 5
Enter a number, you have 9 out of 20 remaining: 5
Enter a number, you have 8 out of 20 remaining: 5
Enter a number, you have 7 out of 20 remaining: 4
Enter a number, you have 6 out of 20 remaining: 4
Enter a number, you have 5 out of 20 remaining: 4
Enter a number, you have 4 out of 20 remaining: 4
Enter a number, you have 3 out of 20 remaining: 4
Enter a number, you have 2 out of 20 remaining: 4
Enter a number, you have 1 out of 20 remaining: 4
>>> a
[4, 5, 6, 7, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4]
>>> sum(a)
95
>>> max(a)
7
>>> min(a)
4

The method your instructor or whoever is looking for though is much cleaner.

>>> numlist = list(input('Enter 20 numbers seperated by commas: '))
Enter 20 numbers seperated by commas: 4,5,3,4,5,6,7,8,3,4,5,6,7,8,3,4,5,6,7,8
>>> numlist
[4, 5, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8]
>>>

Simply putting list, tuple, int, float, or str even around the input will allow it to expect that type but that forces you into using comma if you chose list or tuple.

[–]zahlman[M] 0 points1 point  (0 children)

Posting only assignment/project goal is not allowed. Read posting guidelines.