python 2 /列表/ if语句/用户输入

时间:2019-03-05 17:00:07

标签: python-2.x

European_countries = ['Germany' , 'France' , 'United Kingdom' , 'Italy' , 'Spain' , 'Switzerland' , 'Netherlands' , 'Greece' , 'Poland' , 'Ukraine' , 'Austria' , 'Belgium' , 'Sweden' , 'Croatia' , 'Norway' , 'Iceland' , 'Romania' , 'Denmark' , 'Malta' , 'Czech Rebublic' , 'Republic of Ireland' , 'Hungary' , 'Finland' , 'Bulgaria' , 'Cyprus' , 'Luxembourg' , 'Serbia' , 'Albania' , 'Slovenia' , 'Lithuania' , 'Montenegro' , 'Slovakia' , 'Estonia' , 'Latvia' , 'Macendonia' , 'Belarus' , 'Monaca' , 'Vatican City' , 'Moldova' , 'Bosnia and Herzegovia' , 'Gibraltar' , 'Andorra' , 'Liechtenstein' , 'Faroe Islands' , 'San Marino' , 'Isle of Man' , 'Aland Islands' , 'Guernsey' , 'Kingdom of the Netherlands' , 'Svalbard and Jan Mayen']

user = input('what is a European country. be exact ')

if user == European_countries [0:39]:

  print('yes')

else:

  print('nope')

当我从控制台中的列表中输入某项时,它总是转到else语句。它永远都不适用于印刷版。

谢谢您的帮助,我正在课堂上做一些小型项目,需要此帮助。

1 个答案:

答案 0 :(得分:0)

您要测试的是用户是否在European_countries中。在python中,您可以这样做

if user in European_countries:
    print('yes')
else:
    print('nope')