0

I'm quite new in Python, and am learning through FreeCodeCamp. This website is wonderful but that's not the point here.

I'm trying (for learning purpose), to create a little program, that pick the greatest and smallest number of a list given as an input :

smallest = None
greatest = None
print(' Type "Done" when you want the program to stop.')

while True:
    value = input('Enter your number :')
    if value == 'Done' or value == 'done':
        break
try:
    int(value)
except:
    print('Please enter a number')
    continue
if smallest is None:
    smallest = value
if greatest is None:
    greatest = value

if value < smallest:
    smallest = value
    print ('Smallest :', smallest, 'Greatest :', greatest)
    continue
if value > greatest:
    greatest = value
    print ('Smallest :', smallest, 'Greatest :', greatest)
    continue
if greatest >= value >= smallest :
    print ('Smallest :', smallest, 'Greatest :', greatest)
    continue

print('Smallest is :', smallest, ' Greatest is :', greatest)

The problem I am encountering, is that it looks like the < & > operators are only comparing the first digit, then the second ...

Here's the result I have :

Enter your number :123
Smallest : 123 Greatest : 123
Enter your number :2
Smallest : 123 Greatest : 2
Enter your number :123
Smallest : 123 Greatest : 2
Enter your number :done
Smallest is : 123  Greatest is : 2

I don't understand this behavior, so maybe someone here will get the answer !

Thank you and have a nice day

gtnchtb
  • 1
  • 1
  • 2
    You’re comparing strings lexicographically, not numbers. Your casting to an int does nothing because you’re not storing the result. – deceze Sep 14 '21 at 17:58
  • you need to store the result of the parsing `number = int(value)` – Toerktumlare Sep 14 '21 at 18:00
  • Ho my god I'm so dumb haha thank you. I thought my line " int(value)" was working but it's incomplete. – gtnchtb Sep 14 '21 at 18:53

0 Answers0