从列表中选择

2024-05-03 20:10:27 发布

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

我希望能够接受来自字符串列表的用户输入作为参数,例如

x = get_user_input(['yes', 'no', 'maybe'])

如果用户没有从列表中输入输入。最后,程序应该返回用户选择的列表部分。你知道吗


Tags: no字符串用户程序列表input参数get
1条回答
网友
1楼 · 发布于 2024-05-03 20:10:27
def get_user_input(allowed_choices):
    while True:
        s = raw_input("Enter your choice " + "/".join(allowed_choices) + ": ")
        if s in allowed_choices:
            return s

x = get_user_input(['yes', 'no', 'maybe'])

如果要进行不区分大小写的比较,请使用s.lower() in allowed_choices。你知道吗

相关问题 更多 >