-1

我让我的程序询问你想要什么攻击,它应该从一个从 's 获得攻击的列表中获取咒语,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()
4

2 回答 2

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

player_spells是一个集合而不是一个列表,并且在您尝试存储的元素之一内部的某个位置也是set导致您的错误的一个。

如果你想要一个列表,你可以用[]not创建它{}

  player_spells = [fire_spells, ice_spells, water_spells, chaos_spells, nature_spells, spirit_spells, equilibrium_spells, intellectual_spells]

我认为关于列表if/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
于 2014-10-05T17:43:14.773 回答
0

集合允许您进行交集、并集、差分和对称差分等运算,即数学集合论的运算。集合不允许在哈希表上实现索引。

列表实际上是可变长度数组,而不是 Lisp 样式的链表。在列表中,元素通过索引访问。除了这个 padraic 还建议你使用 set not list。所以你得到一个错误

有关更多信息,请参阅set 和 list 之间的区别

于 2014-10-05T17:49:51.183 回答