Python 3.x 计算器。我想添加一个循环,上面写着“invalid entry”,如果用户输入的字符不正确,我会重新询问这个问题。不知道怎么做

Python 3.x 计算器。我想添加一个循环,上面写着“invalid entry”,如果用户输入的字符不正确,我会重新询问这个问题。不知道怎么做,python-3.x,loops,calculator,Python 3.x,Loops,Calculator,我想添加一个循环函数,该函数基本上会说“invalid entry”,如果用户输入了无效字符,则会再次循环回问题。替换该行 print("Welcome to the calculator!") print("Enter in the symbol for you desired equation") math = input("Would you like to ( + ) , ( - ) , ( / ) or ( * )?\n")

我想添加一个循环函数,该函数基本上会说“invalid entry”,如果用户输入了无效字符,则会再次循环回问题。

替换该行

print("Welcome to the calculator!")
print("Enter in the symbol for you desired equation")

math = input("Would you like to ( + ) , ( - ) , ( / ) or ( * )?\n")


if math == '*':
    mult = float(input("Give me the first number you would like to multiply\n"))
    mult1 = float(input("Give me the second number\n"))
    print(mult , "*" , mult1 , "=" , float(mult * mult1))

if math == '/':
    div = float(input("Give me the first number you would like to divide\n"))
    div1 = float(input("Give me the second number\n"))
    print(div , "/" , div1 , "=" , float(div / div1))

if math == '+':
    add = float(input("Give me the first number you would like to add\n"))
    add1 = float(input("Give me the second number\n"))
    print(add , "+" , add1 , "=" , float(add+add1))

应该让你有你想要的行为。
break
命令退出循环


您应该能够将其推广到其他输入语句。

您是否尝试添加for或while循环?谢谢!这正是我需要的。我在玩循环,但我能找到它!谢谢。
math = input("Would you like to ( + ) , ( - ) , ( / ) or ( * )?\n")
while True:
    math = input("Would you like to ( + ) , ( - ) , ( / ) or ( * )?\n")
    if math in "+-*/":
        break
    else:
        print('invalid entry')