Python Forum
[split] Python for Everybody 5.2 assignment - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Homework (https://python-forum.io/Forum-Homework)
+--- Thread: [split] Python for Everybody 5.2 assignment (/Thread-split-Python-for-Everybody-5-2-assignment--19144)



[split] Python for Everybody 5.2 assignment - jonchanzw - Jun-14-2019

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!


RE: [split] Python for Everybody 5.2 assignment - micseydel - Jun-14-2019

Can you be more specific? What inputs are you using? How exactly is the behavior different from what you want?


RE: [split] Python for Everybody 5.2 assignment - nilamo - Jun-14-2019

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


RE: [split] Python for Everybody 5.2 assignment - Arjun - Oct-21-2019

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)


RE: [split] Python for Everybody 5.2 assignment - perfringo - Oct-22-2019

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