1
min1=0
max1=0

while True:
    num=input("enter a number")
    if (num=='done'):
        break
    elif (num.isdigit()==False):
        print("sorry enter integers or done")
    elif (int(num)>max1):
        max1=num
    elif (int(num)<min1):
        min1=num
print(max1)
print(min1)

This is throwing error '>' not supported between instances of 'int' and 'str'. I cannot take integer input as I have to break when the user enters done. Also, I have to account for non-integer values entered by user. How to rectify this code.

PySaad
  • 1,052
  • 16
  • 26
Priya
  • 23
  • 4

2 Answers2

2

Assume num='5'. Then elif (int(num)>max1): holds and you assign '5' to max1. In the next iteration, max1 is a string and therefore you get an error. The solution is - max1=int(num) (and same for min1).

Tom Ron
  • 5,906
  • 3
  • 22
  • 38
1

Another way to solve this is to convert the input to an integer once rather than using int(num) multiple times throughout the code. This makes it easier to read later on.

An example of this is is changing

elif (num.isdigit()==False):

to

elif (num.isdigit()==True): or elif (num.isdigit()) (they do the same thing).

Then you can convert the string to an integer with num = int(num)

Example:

min1=0
max1=0

while True:
    num=input("enter a number")
    if (num=='done'):
        break
    if num.isdigit():
        num = int(num)
    else:
        print("sorry enter integers or done")
        continue
    if (num>max1):
        max1=num
    elif (num<min1):
        min1=num
print(max1)
print(min1)
Michael
  • 110
  • 4