如何将 python 对 int(input('Q1')) 中的字符串输入的响应更改为循环 Q?

How to change pythons response to a string input in a int(input('Q1')), to a looped Q?

我的问题是,例如当我键入

x = int(input('whats 1+1')

如果有人开玩笑地输入 'two' 或 '2' python 之类的字符串会自动崩溃,有没有办法改为 python,告诉他输入它作为一个整数,然后使用一个 while 循环,重复这个问题,直到他们这样做。提前致谢:)

顺便说一句,输入时变量 (x) 必须是整数

def get_int(prompt="Enter an Integer:"):
    while True:
        try:
           return int(input(prompt))
        except ValueError:
           print("That is not an integer!")

x = get_int("What is 1+1")
def get_integer():  # Define a function to make things easier
    while True:  # Keep looping, until we return from the function
        x = input('What is 1 + 1?')  # ask for input
        try:
            x = int(x)  # can we convert it to an integer?
        except (ValueError, TypeError):  # None or 'hello' would fail
            print('Invalid entry, please try again!')
        else:  # no exception!
            print('x was good!')
            return x  # exit our function

value = get_integer()  # Call our function