+ 2
Having a problem with getting maximum minimum result
I want to get output "Maximum is 10, Minimum is 2" with input of [7,2,10,4] . And the code actually works if I use other numbers like [7,2,9,4] or even [7,2,99,4] but with number 10 something goes wrong and I get 7 as maximum and 10 as minimum. largest = None smallest = None while True: the_bum = input ("Enter a number: ") if the_num == "done" : break try: fval = int(the_num) except: print('Invalid input') continue if smallest is None: smallest = the_num elif the_num < smallest: smallest = the_num If largest is None: largest = the_num elif the_num > largest: largest = the_num print ('Maximum is', largest) print ('Minimum is ', smallest)
4 Réponses
+ 3
u should compare fval as integer and not the_num as string.
This way you get a lexicographical comparison wich makes 10099 < 2
+ 3
your input still in String
convert it to int
+ 2
Oh, thank you a lot, have a good day♥️
+ 1
I thought by "try: fval = int(the_num) " I converted it to an int? 😬