0

I don't understand.

I thought TypeError was what I needed. I looked at some examples online and I thought this was right:

def main():
    x = int(input("Input a number:"))
    y = int(input("Input another number:"))
    result = x * y 
    while not result: 
        try:
            result = x * y
        except TypeError: 
            print('Invalid Number')
main()
James K
  • 3,692
  • 1
  • 28
  • 36
Laptic
  • 1
  • 1

1 Answers1

0

Include input's in try and except statements

def main():
    while True:
        try:
            x = int(input("Input a number:"))
            y = int(input("Input another number:"))
            break
        except ValueError:
            print('invalid Number')
    result=x*y
    print(result)
R__raki__
  • 847
  • 4
  • 15
  • 30
  • 1
    Thank you! So Whenever I'm using try and except... I have to always put the variables in the try method? – Laptic Oct 23 '16 at 15:23
  • You have to put the lines that may cause an exception in the `try` block. In your code, the exception will be raised by the `int` function. So only the two input lines need to be inside the try block. You would then need to loop around the try: except blocks, and escape from the loop after a valid input is received. See http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response?rq=1 – James K Oct 23 '16 at 20:57