-2

Whenever I test my calculator to see how it deals with inputs that are not numbers, it flags up a ValueError. More precisely, this one "ValueError: could not convert string to float: 'a'". I have tried to amend this so there is a solution to dealing with non-integers but to no avail... Help is much appreciated.

Here is my code so far:

print("1. ADDITION ")
print("2. MULTIPLICATION ")
print("3. TAKEAWAY ")
print("4. DIVISION ")

Operator = int(input("please enter one of the numbers above to decide the operator you want. "))

while Operator > 4:
    print("Please select a number from the options above. ")
    Operator = int(input("please enter one of the numbers above to decide the operator you want. "))
    if Operator != (1 or 2 or 3 or 4):
        print("please enter one of the options above. ")
        Operator = int(input("please enter one of the numbers above to decide the operator you want. "))
    continue

while True:
    Num1 = float(input("Please enter first number. "))
    if Num1 == float(Num1):
        print("thanks! ")
    elif Num1 != float(Num1):
        Num1 = float(input("Please enter first number. "))
    break

while True:
    Num2 = float(input("Please enter second number. "))
    if Num2 == float(Num2):
        print("thanks ")
    elif Num1 != float(Num1):
        Num1 = float(input("Please enter first number. "))
    break
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103

3 Answers3

1

Exception handling is what are you looking for. Smthng like this:

input = 'a'
try:
    int(a)
except ValueError:
    print 'Input is not integer'
poko
  • 575
  • 2
  • 8
0

Please read the docs for handling exceptions:

https://docs.python.org/2/tutorial/errors.html#handling-exceptions

for a float:

try:
    x = float(input("Please enter a number: "))
    break
except ValueError:
    print "Oops!  That was no valid number.  Try again..."
brunoelottero
  • 80
  • 1
  • 7
0

Note: You should name your variables in lower case according to PEP8, see: What is the naming convention in Python for variable and function names?.

If you want to retry an input until you don't get ValueError, put it in a loop:

print("Please select a number from the options above.")
while True:
    try:
        operator = int(input("please enter one of the numbers above to decide the operator you want: "))
    except ValueError:
        print("Sorry, not a number. Please retry.")
    else:
        if 1 <= operator <= 4:
            break
        else:
            print("Sorry, choose a value between 1 and 4.")

Note: use input with Python 3.x or raw_input with Python 2.7

1. ADDITION
2. MULTIPLICATION
3. TAKEAWAY
4. DIVISION
Please select a number from the options above.
please enter one of the numbers above to decide the operator you want: spam
Sorry, not a number. Please retry.
please enter one of the numbers above to decide the operator you want: 12
Sorry, choose a value between 1 and 4.
please enter one of the numbers above to decide the operator you want: 2
Community
  • 1
  • 1
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103