一个简单的计算器。我不想在用户键入 1-4 以外的数字后显示错误消息。它处于一个 while 循环中

A simple calculator. I want t show an error message, after the user types a number other than 1-4. It is in a while loop

我想在用户输入程序中没有的数字 5 时显示错误。这个你能帮我吗。我也想试试这些 如果用户再次键入数字 1、2、3 或 4,则显示错误 我还想在用户键入任何其他内容(例如字母)时显示错误

 def add(num1, num2):
        return num1 + num2
    def sub(num1, num2):
        return num1 - num2
    def mult(num1, num2):
        return num1 * num2
    def div(num1, num2):
        return num1 / num2
    print("""
    Choose Operation:
    1. Addition
    2. Subtraction
    3. Division
    4. Multiplication
            """
            )
    while True:
        sct = int(input("Enter 1,2,3, or 4"))
        number_a = int(input("Please enter first number"))
        number_b = int(input("Please enter second number"))
        if sct == 1:
            print(number_a, "+", number_b, "=", add(number_a, number_b))
        elif sct == 4:
            print(number_a, "X", number_b, "=", mult(number_a, number_b))
        elif sct == 3:
            print(number_a, "/", number_b, div(number_a, number_b))
        elif sct == 2:
            print(number_a, "-", number_b, sub(number_a, number_b))
        else:
            print("error")

这就是我的处理方式:

correct_choices = ['1', '2', '3', '4']
user_choice = 'wrong'
prompt = """
Choose Operation:
1. Addition
2. Subtraction
3. Division
4. Multiplication
: """
while user_choice not in correct_choices :
    user_choice = input (prompt)

如果他们输入 5,我不确定你所说的错误是什么意思,因为它已经这样做了,但你可以通过以下方式再次输入数字:

在开头定义一个列表:

ns_done = []

然后,每次有人选择一个号码时添加到列表中:

sct = int(input("Enter 1,2,3, or 4"))
if ns_done contains sct:
    print("error 123")
    continue

然后,如果没有,则将号码添加到列表中:

ns_done.append(sct)