lerugray
lerugray

Reputation: 369

Value error stumping a newbie

Im trying to get this extra credit exercise done for LPTHW and I've hit a wall. I keep getting a value error and I cant tell why. I know I should be using dict's and classes but i havent gotten up to that part in the book yet.

The idea is that I would like to get this inventory function to remove the previous weapon from the list character_sheetand replace it with the weapon the user just bought. here is what i believe is the relevant code, let me know if im missing anything.

thanks in advance, stack overflow has really helped me as i grapple with learning python from no previous experience.

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)

here is the next bit of relevant code.

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)

Here are the lists im dealing with.

# 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']

And here is my output, I dont get it, let me know if i should add more code.

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$ 

EDIT: here is my whole buy weapon method.

# 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))

Upvotes: 0

Views: 106

Answers (1)

Andrew Gorcester
Andrew Gorcester

Reputation: 19973

Without seeing more of the code I can't say for sure, but it looks like the problem is that when you add a weapon, you are doing character_sheet.append("Current Weapon %s" % current_weapon) but later you're trying to character_sheet.remove(current_weapon).

The first operation there will add a string to the list that looks like "Current Weapon dagger". The second operation will try to remove the string "dagger" from the list. But the exact string "dagger" is not in the list -- "Current Weapon dagger" is. So Python is crashing because it's told to remove "dagger" from the list, but it can't find it -- it can only find "Current Weapon dagger" which doesn't match.

There are a lot of ways to solve this problem because there are a lot of things strange about your code, presumably because you're diligently working your way through the sections of Learning Python the Hard Way in order and you haven't learned how to generalize your code into functions, about the Don't Repeat Yourself principle (all of those if/elif/elif blocks are doing essentially the same thing, so that's "repeating yourself") and how to store data in appropriate data structures.

I recommend you just move on with the lessons and set this project aside, and then, if you're still interested in it later, refer to it every so often to see how you can apply the new stuff you learn as you go. Every few lessons you'll learn something new that will let you simplify this code -- for instance, using objects instead of strings for the weapons; formatting the output into human readable text only when it's viewed, and not when you store it; turning the character sheet into an object or a dictionary instead of a list; and using if/elif/elif blocks for comparison only and generalizing the business logic of adding and removing weapons from your character sheet into a function.

If you just want to fix this problem, then you need to make the string that you're deleting match the string that's in the list somehow. Either put something different into the list (just current_weapon instead of the longer string) or actually search for the longer string itself. Or you can simply delete the last item in the list, if you're sure that's going to be the current_weapon data.

Upvotes: 3

Related Questions