当获取用户输入时,我想检查两个值,但是,我使用的代码会不断循环

2024-04-19 22:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

action = input('Please enter add or del.\n')
def check_greeting1(action):
    while True:
        if action == '':
            action = input("please enter add or del.\n")
        else:
            break

当获取用户输入时,我想检查两个值,但是,我使用的代码会不断循环


2条回答

请做吧。此函数反复询问用户,直到用户输入有效的输入值

def get_input():
    while True:
        inputstr = input('Please enter add or del: ')
        if inputstr == 'add' or inputstr == 'del'
            return inputstr
        else:
            print('Wrong Input!')

action = input('Please enter add or del.\n')放入while循环

def check_greeting1():
    while True:
        action = input('Please enter add or del.\n')
        if not action:
            action = input("please enter add or del.\n")
        else:
            break

相关问题 更多 >