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

Question:

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

Asked By: Kosstheboss123

||

Answers:

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(...)
Answered By: Guy

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.')
Answered By: anthony yaghi

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}

Answered By: Xola
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.