Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python-我可以从原始输入中调用列表名吗?_Python 2.7_List_Class_Raw Input - Fatal编程技术网

Python 2.7 Python-我可以从原始输入中调用列表名吗?

Python 2.7 Python-我可以从原始输入中调用列表名吗?,python-2.7,list,class,raw-input,Python 2.7,List,Class,Raw Input,我正在尝试使用python创建一个游戏,但在我的“类角色:”,我想做如下操作: answer = raw_input('Which class do you want to play') if answer = list_name[0] self.stats = list_name 谢谢大家! 首先请注意,if语句应该有一个==而不是= 这种情况下使用python的in语句也是一个很好的时机,它将检查一个值是否与列表中的任何项匹配!您可以尝试以下方法: list_name = ['classA

我正在尝试使用python创建一个游戏,但在我的“类角色:”,我想做如下操作:

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

首先请注意,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'

假设我的代码是:Warrior_Stats=['Warrior',HP']Hunter_Stats=['Hunter',HP']如果我希望我的原始输入是Warrior或Hunter,我如何检查每个列表,然后返回列表名^@WilliamNi这回答了你的问题,你应该接受它作为answer@WilliamNi我不确定我是否完全理解。如果您编辑您的问题以添加一些代码或伪代码来完成您试图执行的操作,我可能会提供更好的帮助