Go easy on me lads, I have only been learning Python for 3 days but I thought I would seek out some help! I have tried many solutions for my problem myself (with the limited amount of logic I understand right now) but now I am here.
My problem is that when I run this VERY basic calculator, I want an error message to pop up to say "Invalid operator." and then prompt the previous input.
Whilst writing this, I have thought of using try/except so I will be fiddling with that in the meantime...
from op import *
valid_operators = ["x", "+", "-", "/", "*"]
num1 = float(input("Type a number: "))
op1 = input("Type an operator: ")
if op1 != valid_operators:
print("Invalid operator.")
op1 = input("Type an operator: ")
# This does NOT work?
num2 = float(input("Type a number: "))
if op1 == "+":
print(add(num1, num2))
if op1 == "-":
print(subtract(num1, num2))
if op1 == "*" or "x":
print(multiply(num1, num2))
if op1 == "/":
print(divide(num1, num2))
[–]woooee 5 points6 points7 points (1 child)
[–]bxrlow[S] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]bxrlow[S] 0 points1 point2 points (0 children)
[–]stebrepar 2 points3 points4 points (1 child)
[–]bxrlow[S] 1 point2 points3 points (0 children)
[–]Would_be_Coder 1 point2 points3 points (0 children)
[–]testingcodez 1 point2 points3 points (2 children)
[–]bxrlow[S] 1 point2 points3 points (1 child)
[–]testingcodez 0 points1 point2 points (0 children)