0

i am attempting to use this loop to determine the max and min values inputted by a user. the provided numbers that are to be inputted are as follows: 7 2 10 4

largest = None
smallest = None

while True :
    num = input('Enter a number: ')
    if num == 'done' :
        break
    try :
        float(num)
    except :
        print('Invalid input')
        continue
    if largest is None :
        largest = num
    elif num > largest:
        largest = num
    if smallest is None :
        smallest = num
    elif num < smallest :
        smallest = num
    print(num, largest, smallest)
print('Maximum is', largest)
print('Minimum is', smallest)

for the first two numbers the code seems to run smoothly, i have the "print(num, largest, smallest)" statement at the bottom of the loop that verifies this.

however when i input "10" it counts it as the minimum value and not the maximum.

i understand that python inputs as strings and one must convert said inputs to integers or floats, however i am sort of lost as to how this seems to work with single digit numbers but once i use 2 digits numbers, they do not read through as maximum values.

hopefully this is a simple problem to fix, any help is greatly appreciated.

  • 1
    `float(num)` does not modify the value in place, you need `num = float(num)` – MegaIng Nov 05 '22 at 18:26
  • hi @MegaIng , thank you for that input it worked this time around. could you elaborate on why this changes the output? i am in my first week or so of python so i am not too sure of the syntax yet. EDIT: this outputs a float (10.0) as the maximum however the answer is supposed to be an integer. this was why i assumed the initial syntax was correct because it only modifies the value whilst in the loop. – James Hill Nov 05 '22 at 18:30
  • Use `int` if you want an integer. – Keith Nov 05 '22 at 20:14

0 Answers0