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

时间:2018-01-29 13:52:14

标签: 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'\

3 个答案:

答案 0 :(得分:0)

您想要列表连接:

while size not in l_size + m_size + s_size:

答案 1 :(得分:0)

删除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")

答案 2 :(得分:0)

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'\