Python Forum

Full Version: [split] Python for Everybody 5.2 assignment
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        fnum = float(num)
    except:
        print("Invalid input")
        continue
    if largest is None :
    	largest = float(num)
    elif num > largest :
        largest = float(num)
    if smallest is None :
        smallest = float(num)
    elif num < smallest :
    	smallest = float(num)
    print(num)
    print("Min:",smallest)
    print("Max:",largest)
print("Maximum is",int(largest))
print("Minimum is",int(smallest))
Can someone help point to where I have gone wrong with the logic please? Thanks!
Can you be more specific? What inputs are you using? How exactly is the behavior different from what you want?
(Jun-14-2019, 04:38 PM)jonchanzw Wrote: [ -> ]elif num > largest :

num is a string. largest is a float. You have an fnum variable available, why not use that for comparisons?
Here the code will help you...……
largest = None
smallest = None
while True:
try:
num = input("Enter the number: ")
if num == "done":
break
num = int(num)
if largest is None or largest < num:
largest = num
elif smallest is None or smallest > num:
smallest = num
except ValueError:
print("Invalid input")

print ("Maximum is", largest)
print ("Minimum is", smallest)
As far as my googling went I understood that there are basically no limitations regarding solution.

So one can take advantage of Python 3.8 new cool feature and of course use built-in min and max to deliver result:

ints = []

while (answer := input('Enter integer: ').lower()) != 'done':
    try:
        ints.append(int(answer))
    except ValueError:
        print(f"{answer!r} can't be interpreted as integer")

print(f'Maximum entered value was {max(ints)} and minimum value was {min(ints)}')