Home > Enterprise >  Can't seem to get the str to change to an int with this custom game? Python 3.10
Can't seem to get the str to change to an int with this custom game? Python 3.10

Time:08-16

#A small game

goblin = 20

dagger = 10

sword = 15

axe = 25

print('There is a goblin up ahead, what weapon would you like to choose? A dagger, a sword, or an axe?')

weapon = input()


print('You look around, and pick up the '   weapon)


if weapon > goblin:

    #A small game
    goblin = 20
    dagger = 10
    sword = 15
    axe = 25

    print('There is a goblin up ahead, what weapon would you like to choose? A dagger, a sword, or an axe?')
    weapon = input()

    print('You look around, and pick up the '   weapon)

    if weapon > goblin:
        print('You swing the '   weapon   ' at the goblin, cutting its head off in succession!')

    else:
        print('You swing the '   weapon   ' at the goblin, but fail to do enough damage. You are defeated.')


    print('You swing the '   weapon   ' at the goblin, cutting its head off in succession!')

else:
    print('You swing the '   weapon   ' at the goblin, but fail to do enough damage. You are defeated.')

--

I'm trying to make it so that if the player picks an axe, it registers as an int instead of a str. For example, if the player types 'axe', I want it so weapon = 25

Any advice?

CodePudding user response:

You can hold the weapons in a dict and pick them with the input

weapons = {'dagger': 10, 'sword': 15, 'axe': 20}
weapon = input() # 'axe'

if weapons[weapon] > goblin:
    print(...)

CodePudding user response:

You need to use something like a dictionary, maybe a builder pattern will be better for a large scale thing. Here is a working code using dictionaries, you still need to make sure the user input is a valid choice.

weapons = {"dagger": 10, "sword": 15, "axe": 25}
goblin = 20

print('There is a goblin up ahead, what weapon would you like to choose? A dagger, a sword, or an axe?')
weapon = input()
weapon_strength = weapons[weapon]

print('You look around, and pick up the '   weapon)

if weapon_strength > goblin:
    print('You swing the '   weapon   ' at the goblin, cutting its head off in succession!')
else:
    print('You swing the '   weapon   ' at the goblin, but fail to do enough damage. You are defeated.')

CodePudding user response:

put the weapons on a map, so you can use the input as the key to access the values. weapons = {'dagger': 10, 'sword': 15, 'axe': 20}

  • Related