Python 难倒一个新手的值错误

Python 难倒一个新手的值错误,python,Python,我正在努力完成这个额外的学分练习,但我遇到了麻烦。我不断得到一个值错误,我不知道为什么。我知道我应该使用dict和课堂,但我还没有达到书中的这一部分 我的想法是,我想使用这个库存函数从列表character\u sheet中删除以前的武器,并将其替换为用户刚购买的武器。这是我相信的相关代码,如果我遗漏了什么,请告诉我 提前感谢,stack overflow真的帮了我很大的忙,因为我以前没有学习python的经验 weapon_choice = raw_input(":> ")

我正在努力完成这个额外的学分练习,但我遇到了麻烦。我不断得到一个值错误,我不知道为什么。我知道我应该使用dict和课堂,但我还没有达到书中的这一部分

我的想法是,我想使用这个库存函数从列表
character\u sheet
中删除以前的武器,并将其替换为用户刚购买的武器。这是我相信的相关代码,如果我遗漏了什么,请告诉我

提前感谢,stack overflow真的帮了我很大的忙,因为我以前没有学习python的经验

weapon_choice = raw_input(":> ")
        if "sword" in weapon_choice:
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_zero_weapons)
下面是下一段相关代码

weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
这是我正在处理的清单

# Weapon lists 
level_zero_weapons = ['short sword', 'club', 'dagger']
level_one_weapons = ['sword', 'mace', 'rapier']
level_two_weapons = ['long sword', 'morningstar', 'trident']
level_three_weapons = ['claymore', 'flail', 'sycthe']
level_four_weapons = ['bastard sword', 'dragon bone', 'crystal halbred']
这是我的输出,我不明白,让我知道我是否应该添加更多的代码

Please tell me your name brave soul. :> Ray

        Lets now randomly generate brave gladiator Ray.
[                             'Name: Ray:',
                              'Gender: Male',
                              'Character Class: Warrior',
                              'Strength: 11',
                              'Dexterity: 7',
                              'Constitution: 8',
                              'Damage 1D6',
                              'Crit Chance 10%',
                              'Hit Points: 6/6']
Please Press Enter To Buy A Weapon

Please type in the weapon you want to buy.

short sword, price: 1 gold pieces

club, price: 1 gold pieces

dagger, price: 1 gold pieces.

:> dagger

Your current weapon is now a dagger. Press Enter To Continue


Type in the weapon you want to buy, type quit to return to the barracks.

sword, price: 3 gold pieces

mace, price: 4 gold pieces

rapier, price: 5 gold pieces.

:> sword
Traceback (most recent call last):
  File "lodarena.py", line 399, in <module>
    character_gen()
  File "lodarena.py", line 394, in character_gen
    buy_weapon(level_one_weapons)
  File "lodarena.py", line 144, in buy_weapon
    character_sheet.remove(current_weapon)
ValueError: list.remove(x): x not in list
Raymond-Weisss-MacBook-Pro:lodarena Raylug$ 

在没有看到更多代码的情况下,我无法确定,但问题似乎是,当您添加武器时,您正在执行
角色表.append(“当前武器%s”%Current\u武器)
,但稍后您将尝试
角色表.remove(当前武器)

那里的第一个操作将在列表中添加一个类似“当前武器匕首”的字符串。第二个操作将尝试从列表中删除字符串“dagger”。但确切的字符串“匕首”不在列表中--“当前武器匕首”在列表中。因此Python崩溃是因为它被告知从列表中删除“匕首”,但它找不到它——它只能找到不匹配的“当前武器匕首”

有很多方法可以解决这个问题,因为您的代码有很多奇怪的地方,大概是因为您正在努力学习Python的各个部分,而且您还没有学会如何将代码概括为函数,关于“不要重复自己”原则(所有这些if/elif/elif块基本上都在做相同的事情,这就是“重复你自己”),以及如何将数据存储在适当的数据结构中

我建议你继续学习这些课程,把这个项目放在一边,然后,如果你以后还对它感兴趣的话,经常参考它,看看如何在学习的过程中应用你学到的新东西。每隔几节课你就会学到一些新东西,可以简化这个代码——例如,使用对象而不是字符串r武器;仅在查看时将输出格式化为人类可读的文本,而不是在存储时;将字符表转换为对象或字典而不是列表;使用if/elif/elif块仅用于比较,并将从字符表添加和删除武器的业务逻辑概括为一个函数。


如果您只是想解决这个问题,那么您需要使要删除的字符串与列表中的字符串以某种方式匹配。或者将不同的内容放入列表中(只需
当前\u
而不是较长的字符串)或者实际上搜索更长的字符串本身。或者你可以简单地删除列表中的最后一项,如果你确定这将是
当前的武器数据。

没有看到更多的代码,我不能肯定,但问题似乎是,当你添加武器时,你正在执行
字符表.append(“当前武器%s”%Current\u武器)
,但稍后您将尝试
角色表。删除(当前武器)

第一个操作将向列表中添加一个字符串,看起来像“当前武器匕首”。第二个操作将尝试从列表中删除字符串“匕首”。但确切的字符串“匕首”不在列表中--“当前武器匕首”在列表中。因此Python正在崩溃,因为它被告知要删除“匕首”从列表中,但它找不到它-它只能找到不匹配的“当前武器匕首”

有很多方法可以解决这个问题,因为您的代码有很多奇怪的地方,大概是因为您正在努力学习Python的各个部分,而且您还没有学会如何将代码概括为函数,关于“不要重复自己”原则(所有这些if/elif/elif块基本上都在做相同的事情,这就是“重复你自己”),以及如何将数据存储在适当的数据结构中

我建议你继续学习这些课程,把这个项目放在一边,然后,如果你以后还对它感兴趣的话,经常参考它,看看如何在学习的过程中应用你学到的新东西。每隔几节课你就会学到一些新东西,可以简化这个代码——例如,使用对象而不是字符串r武器;仅在查看时将输出格式化为人类可读的文本,而不是在存储时;将字符表转换为对象或字典而不是列表;使用if/elif/elif块仅用于比较,并将从字符表添加和删除武器的业务逻辑概括为一个函数。


如果您只是想解决这个问题,那么您需要使要删除的字符串与列表中的字符串以某种方式匹配。或者将不同的内容放入列表中(只需
当前\u
而不是较长的字符串)或者实际上搜索更长的字符串本身。或者你可以简单地删除列表中的最后一项,如果你确定这将是当前的武器数据。

你购买武器的方法在哪里?请发布相关代码。你的前两个代码看起来很相似。有什么区别吗?我现在将包括所有购买武器的方法。在哪里是你的购买武器方法吗?请发布相关代码。你的前两个代码看起来很相似。有什么区别吗?我现在将包括我所有的购买武器方法。所以你的意思是,如果我想让它在角色表中工作。删除(“当前武器%s”%Current武器)将使它工作,对吗?我会
# Doing Stuff for weapons and the shopkeeper. #################################

def level_zero_price():
    """Generates the price for level one weapons"""
    return randint(1, 3)

def level_one_price():
    """Generates the price for level two weapons"""
    return randint(3, 6)

def level_two_price():
    """Generates the price for level three weapons"""
    return randint(6, 9)

def level_three_price():
    """Generates the price for level four weapons"""
    return randint(9, 12)

def level_four_price():
    "Generates the price for level four weapons"""
    return randint(12, 15)
### Major Buying Stuff / Inventory Code ##########################################

def buy_weapon(weapons):
    """big bit of code that allows you to buy a weapons from a weapon list.
The function acts a little differently after level zero weapons"""
    global current_weapon
    if weapons == level_zero_weapons:
        sword_price = level_zero_price()
        blunt_price = level_zero_price()
        agile_price = level_zero_price()
        print t.bright_yellow_on_magenta + """
Please type in the weapon you want to buy.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2], 
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_zero_weapons)

    elif weapons == level_one_weapons:
        sword_price = level_one_price()
        blunt_price = level_one_price()
        agile_price = level_one_price()
        print"""
Type in the weapon you want to buy, type quit to return to the barracks.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price, weapons[2],
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_one_weapons)

    elif weapons == level_two_weapons:
        sword_price = level_two_price()
        blunt_price = level_two_price()
        agile_price = level_two_price()
        print"""
Type in the weapon you want to buy, type quit to return to the barracks.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2], 
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_two_weapons)

    elif weapons == level_three_weapons:
        sword_price = level_three_price()
        blunt_price = level_three_price()
        agile_price = level_three_price()
        print"""
Type in the weapon you want to buy, type quit to return to the barracks.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2], 
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_three_weapons)

    elif weapons == level_four_weapons:
        sword_price = level_four_price()
        blunt_price = level_four_price()
        agile_price = level_four_price()
        print"""
Type in the weapon you want to buy, type quit to return to the barracks.

%s, price: %d gold pieces

%s, price: %d gold pieces

%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2], 
       agile_price)

        weapon_choice = raw_input(":> ")
        if weapons[0] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[0]
            inventory(weapons[0])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[1] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[1]
            inventory(weapons[1])
            character_sheet.append("Current Weapon %s" % current_weapon)
        elif weapons[2] in weapon_choice:
            character_sheet.remove(current_weapon)
            current_weapon = weapons[2]
            inventory(weapons[2])
            character_sheet.append("Current Weapon %s" % current_weapon)
        else: 
            print "I dont know what %s means" % weapon_choice
            buy_weapon(level_four_weapons)      
    else:
        print"~~~There is a bug somwhere, forgot to assign (weapons)\n\n\n"
    raw_input(t.white_on_red("""
Your current weapon is now a %s. Press Enter To Continue
""" % current_weapon))