Home > Software engineering >  Try and Except not printing line under it python
Try and Except not printing line under it python

Time:10-16

I am making a very basic calculator and I am currently struggling with having my Error Exception print the line under it in python.

My code includes:

loops = int(input("How many math problems do you need: "))

for i in range(loops):
    while True:
        method = input("What math method would you like to use. m = multiplication, d = division, a = addition, s = subtraction:, ex = exponent: ")  
        try: 
            if method in ['m','M','d','D','a','A','s','S','Ex','ex','EX','eX']:
                break
        except:
            print("Error your method is not viable")
    
    while True:
        num1 = input("What is the first number you want to input into your method: ")
        try: 
            num1 = float(num1)
            break
        except ValueError:
            print("Error your first number needs to be a number")

    while True:
        num2 = input("What is the second number you want to input into your method: ")
        try: 
            num2 = float(num2)
            break
        except ValueError:
            print("Error your second number needs to be a number")

    if method in ['m', 'M']:
        finalans = num1 * num2
        print("Your answer is",finalans)
    if method in ['d', 'D']:
        finalans = num1 / num2
        print("Your answer is",finalans)
    if method in ['a', 'A']:
        finalans = num1   num2
        print("Your answer is",finalans)
    if method in ['s', 'S']:
        finalans = num1 - num2
        print("Your answer is",finalans)
    if method in ['Ex', 'ex', 'EX', 'eX']:
        finalans = num1 ** num2
        print("Your answer is",finalans)
    exit()

On line 10 I am having trouble making the except print out the line under it does anyone know what I am doing wrong?

CodePudding user response:

There are tho mistakes I can see the first is that you forgot to indent everything under line 3 for i in range(loops): and to exit(). the second is that if method in [...] dose not throw any errors. So you ether have to replace it with [...].index(method) or replace

try: 
    if method in [...]:
        break
except:
    print("<error message>")

with

if method in [...]:
    break
else:
    print("<error message>")

where [...] is your list of all possible operations

CodePudding user response:

I you want to use exceptions to handle invalid methods you can use it like this:

loops = int(input("How many math problems do you need: "))

for i in range(loops):
    while True:
        method = input("What math method would you like to use. m = multiplication, d = division, a = addition, s = subtraction:, ex = exponent: ")  
        try: 
            if method not in ['m','M','d','D','a','A','s','S','Ex','ex','EX','eX']:
                raise Exception()
            break
        except:
            print("Error your method is not viable")

However is much better to handle it with a simple if-else directive:

loops = int(input("How many math problems do you need: "))

for i in range(loops):
    while True:
        method = input("What math method would you like to use. m = multiplication, d = division, a = addition, s = subtraction:, ex = exponent: ")   
        if method in ['m','M','d','D','a','A','s','S','Ex','ex','EX','eX']:
            break
        else:
            print("Error your method is not viable")
  • Related