-1

How can I keep asking the user to enter valid input until the user enters an integer and non-zero figure for y-value? I'm still practicing While Loop and it would be great if you could help me.

Thank you in advance!

x = 5
y = 0

while y == 0 or y (is not integer??):
    try:
         z = x/y

    except ZeroDivisionError:
        y = int(input('You cannot divide with "0".\nTry using a number bigger than 0 for y-value.'))

    else:
        print(f'dThanks for placing the right value for y. \nYour result is {z}')
        break

    finally:
        print('All Done')
Sho
  • 33
  • 7
  • Does your code do what you want it to do? If not, what happens instead? Do you get an error? Which? –  Feb 12 '19 at 14:01
  • Related reading: [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/953482) – Kevin Feb 12 '19 at 14:06

1 Answers1

0

You could do something like this:

wrong = True
while wrong:
    try:
        number = input("please enter a non-zero number: ")
        if isinstance(int(number), int) and int(number) != 0:
            print('You entered: ' + number)
            y = int(number)
            wrong = False
        else:
            print("make sure it's a nonzero number only!")
    except:
        print("try again please.")

z = x/y

print('The answer is: ' + str(z))
tasha
  • 86
  • 7
  • Thanks for your response. But it keeps reacting 'make sure its a non zero number only!" even when I put appropriate non-zero integer as a y-value. – Sho Feb 17 '19 at 00:25
  • I just edited it (small fix), try now and let me know. – tasha Feb 19 '19 at 15:26
  • Thank you Tasha! this works now! As a beginner, I'm still trying to get a hang of error handling and while loop (especially negative condition (e.g. while not / !=)). This has been a good learning experience. – Sho Feb 20 '19 at 16:11