Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

#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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any advice?

>Solution :

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(...)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading