The problem with your code is that you are taking input.
input()
function in python takes input as a string, so you need to typecast this. I am assuming you wanted to compare the integral values so for that, you should use instead int(input("YOUR MSSG"))
or if you want to typecast to float then just replace int
with float
.
Scenarios where your code will show awkward behavior: -
-> Let's say that you want to compare 11
and 2
, then if you consider this as an integer, obviously 11 > 2
but when you consider the same thing as a string and do a string comparison then you will see that "11" < "2"
. As the code in the question is taking the inputs as string and doing the string comparison that's why you are not getting the expected result.
The below code should work perfectly for you: -
smallest = None
while True:
num = int(input('Enter a number:'))
if smallest is None:
smallest = num
continue
elif num < smallest:
smallest = num
print(smallest)
elif num == "done":
break
You can modify the code as per your requirement.