Python - 我可以从raw_input中调用列表名称吗?

时间:2017-07-18 01:57:25

标签: python-2.7 list class raw-input

我正在尝试使用python创建游戏,但在我的“class Character:”中,我想做类似的事情:

answer = raw_input('Which class do you want to play')
if answer = list_name[0]
self.stats = list_name

谢谢!

1 个答案:

答案 0 :(得分:1)

首先请注意,您的if声明应该有==而不是=

此类情况也是使用python的in语句的好时机,该语句将检查值是否与列表中的任何项匹配!你可以尝试这样的事情:

list_name = ['classA','classB','classC','classD']

answer = raw_input('Which class do you want to play: ')

#Check if the answer is one of the options in your list
if answer in list_name:
    my_stats = answer
    print 'great, your class is '+my_stats

else:
    print 'sorry, ['+answer+'] is not an ok class'