【问题标题】:Python - can i recall a list name from raw_input?Python - 我可以从 raw_input 中回忆一个列表名称吗?
【发布时间】:2017-12-22 16:48:44
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: python-2.7 list class raw-input


    【解决方案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'
    

    【讨论】:

    • 假设我的代码是:Warrior_Stats = ['Warrior', 'HP'] Hunter_Stats = ['Hunter', 'HP'] 如果我希望我的 raw_input 是 Warrior 或 Hunter,该怎么做我检查每个列表,然后返回列表名称^
    • @WilliamNi 这回答了你的问题,你应该接受它作为答案
    • @WilliamNi 我不确定我是否完全理解。如果您编辑问题以添加一些代码或伪代码,我可能会提供更好的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 2021-10-16
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多