How do I display the error code during the production instead of giving it at the end of the program?
Question:
I am new to python and tried to make my own simple calculator script. The goal is to store the input of the mathematical operator, get the first value and the second value and then apply the chosen operator to the values. It worked just fine except that it was throwing the ‘invalid mathematical operator’ error after the program ended. I wanted it to display the error right after the user inputs the wrong operator (ie: not +, -, * or /).
The code doesn’t seem that efficient because I am still learning how to optimize and to find good alternatives instead of spamming if, elif.
# primitive calculator script
error = "Invalid mathematical operation." # global error variable
ops = ["+", "-", "*", "/"]
lark = input("Enter a mathematical operation (+, -, / or *): ")
if lark != ops:
print("Error. Line 8")
quit()
exart = input("Enter the first value: ")
blip = input("Enter the second value: ")
if lark == "+":
print("Sum of these numbers is:", int(blip)+int(exart))
elif lark == "-":
print("Subtraction of these numbers is:", int(blip)-int(exart))
elif lark == "*":
print("Product of these numbers is:", int(blip)*int(exart))
elif lark == "/":
print("Division of these numbers is: ", int(blip)/int(exart))
Answers:
error = "Invalid mathematical operation." # global error variable
ops = ["+", "-", "*", "/"]
lark = input("Enter a mathematical operation (+, -, / or *): ")
if lark not in ops:
print("Error. Line 8")
quit()
exart = input("Enter the first value: ")
blip = input("Enter the second value: ")
if lark == "+":
print("Sum of these numbers is:", int(blip)+int(exart))
elif lark == "-":
print("Subtraction of these numbers is:", int(blip)-int(exart))
elif lark == "*":
print("Product of these numbers is:", int(blip)*int(exart))
elif lark == "/":
print("Division of these numbers is: ", int(blip)/int(exart))
Does this meet your expectations?
if so, you should change your "!=" to "not in" behind if lark
I am new to python and tried to make my own simple calculator script. The goal is to store the input of the mathematical operator, get the first value and the second value and then apply the chosen operator to the values. It worked just fine except that it was throwing the ‘invalid mathematical operator’ error after the program ended. I wanted it to display the error right after the user inputs the wrong operator (ie: not +, -, * or /).
The code doesn’t seem that efficient because I am still learning how to optimize and to find good alternatives instead of spamming if, elif.
# primitive calculator script
error = "Invalid mathematical operation." # global error variable
ops = ["+", "-", "*", "/"]
lark = input("Enter a mathematical operation (+, -, / or *): ")
if lark != ops:
print("Error. Line 8")
quit()
exart = input("Enter the first value: ")
blip = input("Enter the second value: ")
if lark == "+":
print("Sum of these numbers is:", int(blip)+int(exart))
elif lark == "-":
print("Subtraction of these numbers is:", int(blip)-int(exart))
elif lark == "*":
print("Product of these numbers is:", int(blip)*int(exart))
elif lark == "/":
print("Division of these numbers is: ", int(blip)/int(exart))
error = "Invalid mathematical operation." # global error variable
ops = ["+", "-", "*", "/"]
lark = input("Enter a mathematical operation (+, -, / or *): ")
if lark not in ops:
print("Error. Line 8")
quit()
exart = input("Enter the first value: ")
blip = input("Enter the second value: ")
if lark == "+":
print("Sum of these numbers is:", int(blip)+int(exart))
elif lark == "-":
print("Subtraction of these numbers is:", int(blip)-int(exart))
elif lark == "*":
print("Product of these numbers is:", int(blip)*int(exart))
elif lark == "/":
print("Division of these numbers is: ", int(blip)/int(exart))
Does this meet your expectations?
if so, you should change your "!=" to "not in" behind if lark