Why is this code not working? I keep getting "Invalid input" after every integer input, even if they are being considered as integers in the final maximum-minimum part of the code. Also the minimum keeps coming as "None". Please exlain it simply, im new to coding. Thank you.
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
num = int(num)
if largest is None or largest < num:
largest=num
elif smallest > num or smallest is None:
smalllest = num
except:
print("Invalid input")
continue
print("Maximum is", largest)
print("Minimun is", smallest)
I was expecting to see this output: "Invalid input"
only for string text inputs, and the minimum code to work
CodePudding user response:
In the below, you have a typo. smalllest
should be smallest
.
elif smallest > num or smallest is None: smalllest = num
Correcting that we note that "Invalid input" is only printed when you input a smaller number that the largest:
Enter a number: 56
Enter a number: 78
Enter a number: 89
Enter a number: 34
Invalid input
Enter a number: 45
Invalid input
Enter a number: 1
Invalid input
Enter a number: 67
Invalid input
Enter a number: 8999
Enter a number:
Your problem arises from the below. Because of the order of these conditions, smallest
can be compared to num
even if smallest
is None.
elif smallest > num or smallest is None:
Which we can see generates an error:
>>> None > 56
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'NoneType' and 'int'
Changing this to the following resolves the issue.
elif smallest is None or smallest > num:
Please also be aware that catching every possible exception with except:
is a bad idea. Be more specific about which exceptions you're handling. As well, the continue
is unnecessary. Having handled the exception, the loop will continue anyway.
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
num = int(num)
if largest is None or largest < num:
largest = num
elif smallest is None or smallest > num:
smallest = num
except ValueError:
print("Invalid input")
CodePudding user response:
One easy way to debug this is to catch and print your error. You may do so with
...
try:
...
except Exception as e:
print(f"Exception {e}")
...
When you do this, you will see that when you enter a lower number than the max,
Exception '>' not supported between instances of 'NoneType' and 'int'
is printed.
This is due to smallest
being None
. I will leave fixing it as an exercise :)
CodePudding user response:
You have typo of smalllest
->smallest
that's why there is no change in the smallest
variable
you can initialize largest and smallest as -infinity
,infinity
respectively and than you can check for largest and smallest see the below code.
Code:
largest = -float('inf') #Initialize to -inf
smallest = float('inf') #Initialize to inf
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
num=int(num)
if largest<num: #check conditions.
largest=num
if smallest>num:
smallest=num
except:
print("Invalid input")
continue
print("Maximum is", largest)
print("Minimun is", smallest)
Output:
Enter a number: 58
Enter a number: 78
Enter a number: 32
Enter a number: 16
Enter a number: 24
Enter a number: 99
Enter a number: done
Maximum is 99
Minimun is 16