Python 3.x 我的想法-这是正确的编码吗?

Python 3.x 我的想法-这是正确的编码吗?,python-3.x,Python 3.x,我想出了一个主意,我认为这个主意很聪明: userinput = '' while userinput != 'yes': userinput= input('Pick first player randomly? (yes/no) ') if userinput== 'no': break 那真的是“蟒蛇”吗?还有更好或“合适”的方法吗?我会这样重写: while True: userinput=input('Pick first player ran

我想出了一个主意,我认为这个主意很聪明:

userinput = ''
while userinput != 'yes':
    userinput= input('Pick first player randomly? (yes/no) ')
    if userinput== 'no':
        break

那真的是“蟒蛇”吗?还有更好或“合适”的方法吗?

我会这样重写:

while True:
    userinput=input('Pick first player randomly? (yes/no) ').strip().lower()
    if userinput in ('no', 'yes'):
        handle_user_input(userinput)
        break

使用
而True
与set string和test相比更像python。另外,考虑大小写和尾随空格。添加更多函数显然会更好/更容易。谢谢