Reputation:
def main():
print("*** High School Diving ***")
num_judges=int(input("How many judges are there? "))
for i in range (num_judges):
scores=int(input("Ender a score: " ))
x=min(scores)
y=max(scores)
print("Min: ", x)
print("Max: ", y)
main()
Upvotes: 0
Views: 14458
Reputation: 365767
Here are some more ways you could do this.
First, at least two people have already posted the exact same thing as the first answer by Martijn Pieters's answer, and I don't want to feel left out, so:
scores = []
for i in range(num_judges):
scores.append(int(input("Enter a score: ")))
x=min(scores)
y=max(scores)
Now, whenever you create an empty list and append to it in a loop, this is identical to a list comprehension, so:
scores = [int(input("Enter a score: ")) for i in range(num_judges)]
x=min(scores)
y=max(scores)
Meanwhile, what if num_judges
is huge, and you don't want to build that huge list just to find the min and max values? Well, you could keep track of them as you go along:
x, y = float('inf'), float('-inf')
for i in range(num_judges):
score = int(input("Enter a score: "))
if score < x:
x = score
if score > y:
y = score
Finally, is there a way to get the best of both worlds? Usually, this just means using a generator expression instead of a list comprehension. But here, you need both min
and max
to traverse the scores, which means it has to be a list (or something else reusable).
You can get around this with tee
:
scores= (int(input("Enter a score: ")) for i in range(num_judges))
scores1, scores2 = itertools.tee(scores)
x = min(scores1)
y = max(scores2)
However, this doesn't really help, because under the covers, tee
is going to create the same list you would have already created. (tee
is very useful when you're going to traverse two iterators in parallel, but not in cases like this.)
So, you need to write a min_and_max
function, which is going to look a lot like the for
loop in the previous example:
def min_and_max(iter):
x, y = float('inf'), float('-inf')
for val in iter:
if val < x:
x = val
if val > y:
y = val
return x, y
And then, you can do the whole thing in a nice, readable one-liner:
x, y = min_and_max(int(input("Enter a score: ")) for i in range(num_judges))
Of course it's not really a one-liner when you had to write an 8-line function to make it work… except that 8-line function may be reusable in other problems in the future.
Upvotes: 1
Reputation: 49403
You were almost there, you just need to make scores
a list and append to it, then this should work:
def main():
print("*** High School Diving ***")
num_judges=int(input("How many judges are there? "))
#define scores as a list of values
scores = []
for i in range (num_judges):
scores.append(int(input("Ender a score: " ))) #append each value to scores[]
x=min(scores)
y=max(scores)
print("Min: ", x)
print("Max: ", y)
main()
If you take a look at the documentation for max()
and min()
they actually give you the syntax there an note that it requires an iterable type (such as a non-empty string, tuple or list).
Upvotes: 0
Reputation: 213261
You are creating a variable scores
inside the for loop, which won't be visible outside it. Secondly, you are trying to over-writing the value in scores
on each iteration, since scores
is not a list
rather a scalar
type.
You should declare scores
as list
type outside the loop, and inside the loop, append
each score to the list.
scores = []
for i in range (num_judges):
scores.append(int(input("Ender a score: " )))
x=min(scores)
y=max(scores)
Upvotes: 0
Reputation: 1122222
You need to use a list, and append each entered score to it:
scores = []
for i in range (num_judges):
scores.append(int(input("Enter a score: " )))
max()
and min()
will then pick the highest and lowest value respectively from that list.
What you did, instead, was replacing scores
with a new value each time you looped; then try and find the min()
of one integer, which doesn't work:
>>> min(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
By using a list, the min()
function can loop over it (iterate) and find your minimum value:
>>> min([1, 2, 3])
1
Upvotes: 1