-1

i need help with this problem. ask user to enter a number inside an infinite while loop, compare this number to let say 50 (less, greater or equal). but in order to exit this while loop user must enter "exit". i have the following code that is working according to requirement but i want 'exit' (if) statement to be written at the end. doing so definitely causes an error. please feel free for alternative ways.

while True:
x = input('please enter a number to compare or enter "exit" to exit the loop \n')
if x == "exit":
    exit()
elif int(x) > 50:
    print(x, 'is greater than 50')
elif int(x) < 50:
    print(x, 'is less than 50')
else:
    print('the number you entered is 50')
Thunderwood
  • 525
  • 2
  • 9

2 Answers2

1

Well, what happens if the user types fkljhae? A ValueError is raised. And... hang on! This is raised for any non-int input - "exit" satisfies this criterion.

from sys import exit

while True:
    x = input('please enter a number to compare or enter "exit" to exit the loop \n')
    try:
        if int(x) > 50:
            print(x, 'is greater than 50')
        elif int(x) < 50:
            print(x, 'is less than 50')
        else:
            print('the number you entered is 50')
    except ValueError:
        if x == "exit":
            exit()

This isn't particularly good, though; what if print raises a ValueError? Let's refactor it so that only int(x) is in the try: except: block:

from sys import exit

while True:
    text = input('please enter a number to compare or enter "exit" to exit the loop \n')
    try:
        x = int(text)
    except ValueError:
        if text == "exit":
            exit()
    else:
        if x > 50:
            print(x, 'is greater than 50')
        elif x < 50:
            print(x, 'is less than 50')
        else:
            print('the number you entered is 50')

This is better, though "exit" isn't at the bottom any more.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
  • yes it works with try and except but if there is any other string then it shows no error and i doesn't exit, i guess that's out of requirement as i mention explicitly to enter 'exit' to leave the loop – jahanzaib younis Oct 17 '18 at 15:35
  • I'd refactor a bit. There's a lot of code in the `try` block, and the only thing that can raise the exception you are interested in is the call to `int`. (Granted, there's nothing else that *can* raise a `ValueError` in this case, so you aren't masking any other exceptions, but in principle, you want to keep the `try` block as focused as possible.) – chepner Oct 17 '18 at 15:39
  • @chepner Correct. However, that doesn't put `"exit"` last... I'll post a better version, too. – wizzwizz4 Oct 17 '18 at 15:40
  • @jahanzaibyounis You can only accept one answer; the "tick" should go to the answer that answers the question best. You can upvote multiple answers. (Just so you know; I noticed that you clicked both. It's fine to accept another answer, if you feel that it answered your question better.) – wizzwizz4 Oct 17 '18 at 15:41
  • @wizzwizz4 that was a mistake i clicked both, your answer is much more explained and surely helped me with my problem. – jahanzaib younis Oct 17 '18 at 16:39
0

It is because you are trying to parse "exit" which is string to int You can use try and except.

kaan bobac
  • 729
  • 4
  • 8