Having a problem with getting maximum minimum result | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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)

3rd Feb 2020, 8:12 AM
Shamil Erkenov
Shamil Erkenov - avatar
4 Answers
+ 3
u should compare fval as integer and not the_num as string. This way you get a lexicographical comparison wich makes 10099 < 2
3rd Feb 2020, 8:40 AM
Oma Falk
Oma Falk - avatar
+ 3
your input still in String convert it to int
3rd Feb 2020, 8:34 AM
Taste
Taste - avatar
+ 2
Oh, thank you a lot, have a good day♥️
3rd Feb 2020, 8:40 AM
Shamil Erkenov
Shamil Erkenov - avatar
+ 1
I thought by "try: fval = int(the_num) " I converted it to an int? 😬
3rd Feb 2020, 8:37 AM
Shamil Erkenov
Shamil Erkenov - avatar