如何在while循环中插入多个列出的变量?

2024-09-28 21:57:40 发布

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

size = (raw_input ('\nWhat Size do you want?').lower()

l_size =['large' , 'lerge' , 'l', 'larg' ,'lorge' ,] 
m_size =['m' , 'med' , 'medium']
s_size =['s' , 'small' , 'smell', 'smoll']

如果用户输入不正确,我使用它是为了获得正确的输入

while size not in ('l_size', 'm_size','s_size'): 
        print ("\033[1;31m Invalid input! Trying Again\n"),
        size = raw_input("\033[1;32;40m What Size do you want? \n")

如果用户输入错误,我想从一开始就运行程序

if size in l_size:
    print '\nYour coffee is Large! '
elif size in m_size:
    print '\n Your Coffee is Medium'
elif size in s_size:
    print '\nYour Coffee is Small'
else:
    print '\nIncorrect Typing! Type Properly'\

Tags: 用户inyouinputsizerawisdo
3条回答

需要列表串联:

while size not in l_size + m_size + s_size:
size = raw_input("\033[1;32;40m What Size do you want? \n").lower()
l_size = ['large', 'lerge', 'l', 'larg','lorge']
m_size = ['m', 'med', 'medium']
s_size = ['s', 'small', 'smell', 'smoll']

while size not in l_size + m_size + s_size:
       print ("\033[1;31m Invalid input! Trying Again\n"),
       size = raw_input("\033[1;32;40m What Size do you want? \n").lower()
       l_size = ['large', 'lerge', 'l', 'larg','lorge']
       m_size = ['m', 'med', 'medium']
       s_size = ['s', 'small', 'smell', 'smoll']


if size in l_size:
    print '\n Your coffee is Large! '
elif size in m_size:
    print '\n Your Coffee is Medium'
elif size in s_size:
    print '\n Your Coffee is Small'
else:
    print '\n Incorrect Typing! Type Properly'\

删除raw_input()周围的额外括号。你知道吗

size = raw_input('\nWhat Size do you want?').lower()

l_size = ['large', 'lerge', 'l', 'larg','lorge'] 
m_size = ['m', 'med', 'medium']
s_size = ['s', 'small', 'smell', 'smoll']

size不在这些列表中时,您需要重置size,而不是创建新变量num。就像@iBug所说的,您需要串联列表才能循环。你知道吗

while size not in l_size + m_size + s_size:
    print("\033[1;31m Invalid input! Trying Again\n")
    size = raw_input("\033[1;32;40m What Size do you want? \n")

相关问题 更多 >