为什么我的列表出错了?

时间:2014-10-05 17:39:46

标签: python python-3.x text-based

我让我的程序询问你想要什么攻击,它应该从列表中获取来自def的攻击的咒语,并且我收到此错误

    player_spells = {fire_spells, ice_spells, water_spells, chaos_spells, nature_spells, spirit_spells, equilibrium_spells, intellectual_spells}
TypeError: unhashable type: 'set'

我的代码:(对不起它已经很久了,但我试图把它缩减到相关的循环,这就像我可以减少它一样小)

toutorial_dialoug()
enemy_health = 0
player_health = 0
player_max_health = 0

def ember():
    ember = enemy_health - random.randint(95, 125)
def flame():
    flame = enemy_health - random.randint(155, 185)
def inferno():
    inferno = enemy_health - random.randint(250, 285)
def lava_river():
    lava_river = enemy_health - random.randint(300, 400)
def snowflake():
    snowflake = enemy_health - random.randint(75, 100)
def blizard():
    blizard = enemy_health - random.randint(135, 200)
def frostbite():
    frostbite = enemy_health - random.randint(225, 265)
def frozen_lake():
    frozen_lake = enemy_health - random.randint(300, 345)
def tropical_storm():
    tropical_storm = enemy_health - random.randint(125, 145)
def hurricane():
    hurricane = enemy_health - random.randint(200, 255)
def tornado():
    tornado = enemy_health - random.randint(300, 350)
def bubble():
    bubble = enemy_health - random.randint(425, 500)
def raining_cats_and_dogs():
    raining_cats_and_dogs = enemy_health - random.randint(95, 125)
def summon_hades():
    summon_hades = enemy_health - random.randint(155, 185)
def burning_sky():
    burning_sky = enemy_health - random.randint(250, 285)
def swap_pips():
    swap_pips = enemy_health - random.randint(300, 345)
def pans_wraith():
    pans_wraith = enemy_health - random.randint(90, 110)
def bees_knees():
    bees_knees = player_health + 400
def deforestation():
    deforestation = enemy_health - random.randint(200, 255)
def earth_shaker():
    earth_shaker = enemy_health - random.randint(285, 330)
def ghost_touch():
    ghost_touch = enemy_health - 100, player_health + 50
def lost_soul():
    lost_soul = enemy_health - 200, player_health + 100
def death_pixie():
    death_pixie = enemy_health - 300, player_health + 150
def pathogen():
    pathogen = enemy_health - random.randint(412, 445)
def pulse():
    pulse = enemy_health - random.randint(85, 112)
def blind():
    blind = enemy_health - random.randint(185, 215)
def overload():
    overload = enemy_health - random.randint(245, 315)
def equalizer():
    equalizer = enemy_health - random.randint(300, 400)
def acid():
    acid = enemy_health - random.randint(70, 90)
def light_overload():
    light_overload = enemy_health - random.randint(100, 120)
def sound_overload():
    sound_overload = enemy_health - random.randint(200, 220)
def time():
    time = enemy_health - random.randint(300, 330)

fire_spells = {ember(), flame(), inferno(), lava_river()}
ice_spells = {snowflake(), blizard(), frostbite(), frozen_lake()}
water_spells = {tropical_storm(), hurricane(), tornado(), bubble()}
chaos_spells = {raining_cats_and_dogs(), summon_hades(), burning_sky(), swap_pips()}
nature_spells = {pans_wraith(), bees_knees(), deforestation(), earth_shaker()}
spirit_spells = {ghost_touch(), lost_soul(), death_pixie(), pathogen()}
equilibrium_spells = {pulse(), blind(), overload(), equalizer()}
intellectual_spells = {acid(), light_overload(), sound_overload(), time()}
player_spells = {fire_spells, ice_spells, water_spells, chaos_spells, nature_spells, spirit_spells, equilibrium_spells, intellectual_spells}
def choose_school():
    have_school = False
    while False:
        input_str = input()
        if input_str.lower().startswith("I"):
            player_spells = player_spells[2]
            player_max_health = 525
            player_health = 525
            player_level = 1
            have_school = True
        elif input_str.lower().startswith("F"):
            player_spells = player_spells[1]
            player_max_health = 425
            player_health = 425
            player_level = 1
            have_school = True
        elif input_str.lower().startswith("W"):
            player_spells = player_spells[3]
            player_max_health = 400
            player_health = 400
            player_level = 1
            have_school = True
        elif input_str.lower().startswith("C"):
            player_spells = player_spells[4]
            player_max_health = 425
            player_health = 425
            player_level = 1
            have_school = True
        elif input_str.lower().startswith("N"):
            player_spells = player_spells[5]
            player_max_health = 465
            player_health = 465
            player_level = 1
            have_school = True
        elif input_str.lower().startswith("S"):
            player_spells = player_spells[6]
            player_max_health = 450
            player_health = 450
            player_level = 1
            have_school = True
        elif input_str.lower().startswith("E"):
            player_spells = player_spells[7]
            player_max_health = 480
            player_health = 480
            player_level = 1
            have_school = True
        elif input_str.lower().startswith("T"):
            player_spells = player_spells[8]
            player_max_health = 500
            player_health = 500
            player_level = 1
            have_school = True
choose_school()
if player_health > player_max_health:
    while True:
        player_health - 1

if player_spells == ice_spells:
    school_name = 'Ice wizard'
if player_spells == fire_spells:
    school_name = 'Fire wizard'
if player_spells == water_spells:
    school_name = 'Water wizard'
if player_spells == chaos_spells:
    school_name = 'Chaos wizard'
if player_spells == nature_spells:
    school_name = 'Nature wizard'
if player_spells == spirit_spells:
    school_name = 'Spirit wizard'
if player_spells == equilibrium_spells:
    school_name = 'Equilibrium wizard'
if player_spells == intellectual_spells:
    school_name = 'Ice wizard'

print('are you sure you want to be a %s' % (school_name))
input_str = input()
if input_str.lower().startswith("N"):
    choose_school()

2 个答案:

答案 0 :(得分:0)

player_spells = {fire_spells, ice_spells, water_spells, chaos_spells, nature_spells, spirit_spells, equilibrium_spells, intellectual_spells}

player_spells是一个不是列表的集合,并且您尝试存储的其中一个元素中的某个位置也是set,这会导致您的错误。

如果您需要列表,请使用[]而不是{}

创建列表
  player_spells = [fire_spells, ice_spells, water_spells, chaos_spells, nature_spells, spirit_spells, equilibrium_spells, intellectual_spells]

我认为 listsif/elif/else上的教程不会受到影响。

使用elifs代替所有if:

if player_spells == ice_spells:
    school_name = 'Ice wizard'
elif player_spells == fire_spells:
    school_name = 'Fire wizard'
elif player_spells == water_spells:
    school_name = 'Water wizard'
elif player_spells == chaos_spells:
    school_name = 'Chaos wizard'
elif player_spells == nature_spells:
    school_name = 'Nature wizard'
elif player_spells == spirit_spells:
    school_name = 'Spirit wizard'
elif player_spells == equilibrium_spells:
    school_name = 'Equilibrium wizard'
elif player_spells == intellectual_spells:
    school_name = 'Ice wizard'
else:
    school_name = "whatever" # in case none are True you need to set a value for school_name

答案 1 :(得分:0)

  

集允许你进行交集,联合等操作   差异和对称差异,即数学集的运算   理论。集合不允许索引在哈希表上实现。

     

列表是真正的可变长度数组,而不是Lisp风格的链接   名单。在列表中,元素由索引访问。除此之外   padraic还建议你使用set not list。所以你是   得到错误

有关更多信息,请参阅集合和列表

之间的difference