0

I am trying to make my first functioning python program and pretty much have it done. I want to make an error message for when a letter is entered instead of a number in the integer input, rather than the code just stopping entirely. Maybe even a new input saying to enter a valid input? Here is my code:

while True:
 print('Basic Calculator')
 op = input('Operation: ')

 if op in ['x', '*']:
     num1 = int(input('First factor: '))
     num2 = int(input('Second Factor: '))
     print('Product:', num1 + num2)

 if op in ['/']:
     num1 = int(input('Dividend: '))
     num2 = int(input('Divisor: '))
     print('Quotient:', num1 / num2)

 if op in ['+']:
     num1 = int(input('First addend: '))
     num2 = int(input('Second Addend: '))
     print('Sum:', num1 + num2)

 if op in ['-']:
     num1 = int(input('Subtrahend: '))
     num2 = int(input('Minuend: '))
     print('Difference:', num1 - num2)

 if not op in ['x', '*', '/', '+', '-']:
     print('{} is an invalid operator.'.format(op))
  • Use `try ... except` to catch the exception thrown when invalid input is entered. Use a loop to repeatedly ask for the input. – Alex Hall May 19 '16 at 23:07
  • I tried doing that but can't figure out a way that would work with it. – Avocado Tech May 19 '16 at 23:22
  • It's important to show what you tried and have us look at why it failed rather than asking us how to do it. The linked duplication question contains plenty of information on how what to do. Try your best to adapt it and then post a question with the code that isn't working so that it can be fixed. – Alex Hall May 19 '16 at 23:29

0 Answers0