-1

This is my code for a shop in my Python RPG game. Whatever value I select, the code executes the things for if I typed dagger. It also never tells me that I have insufficient funds. Always the same answer

global gcredits
dagger = Item('Iron dagger', 5, 5)
sword = Item('Iron sword', 10, 12)
armour = Item('Iron Armour', 15, 20)


print ("Welcome to the shop! Buy all your amour and weapon needs here!")
print ("You have",gcredits,"Galactic Credits!")

print (dagger.name,': Cost:', dagger.value,'Attack points:', dagger.hvalue)
print (sword.name,': Cost:', sword.value,'Attack points:', sword.hvalue)
print (armour.name,': Cost:', armour.value,'Attack points:', armour.hvalue)

choice = input('What would you like to buy?').upper()

if choice == 'DAGGER' or 'IRON DAGGER' or 'IRONDAGGER':
    print ("You have selected the Iron Dagger.")
    if gcredits >= 5:
        print ('Purchase successful')
        gcredits = gcredits - 5

        dEquip = True
        shop()
    elif gcredits < 5:
        print ("You have got insufficient funds")
        shop()

elif choice == 'SWORD' or 'IRON SWORD' or 'IRONSWORD':
    if gcredits >= 10:
        print ('Purchase successful')
        gcredits = gcredits - 10

        sEquip = True
        shop()
    elif gcredits < 10:
        print ("You have got insufficient funds")
        shop()

elif choice == 'ARMOUR' or 'IRON ARMOUR' or 'IRONARMOUR':
    if gcredits >= 15:
        print ('Purchase successful')
        gcredits = gcredits - 15

        aEquip = True
        shop()
    elif gcredits < 15:
        print ("You have got insufficient funds")
        shop()

else:
    print ("That is not an item. Try again.")
    shop()
Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
myhotchoc
  • 11
  • 1
  • 6

1 Answers1

1

The way you write your OR condition is wrong:

if choice == 'DAGGER' or 'IRON DAGGER' or 'IRONDAGGER':

It is supposed to be:

if choice == 'DAGGER' or choice == 'IRON DAGGER' or choice == 'IRONDAGGER':

or, more Pythonically:

if choice in ('DAGGER', 'IRON DAGGER', 'IRONDAGGER'):

When you do if choice == 'DAGGER' or 'IRON DAGGER' what happen is that you do not check if

  1. your choice is DAGGER is True or if
  2. your choice is IRON DAGGER is True

but you check if

  1. your choice is DAGGER is True or
  2. if IRON DAGGER is True

Note that if 'IRON DAGGER' will always return True:

if 'IRON DAGGER': #this is always true
Ian
  • 30,182
  • 19
  • 69
  • 107
  • Or since it's Python, you can avoid repeating the variable name over and over with `if choice in ('DAGGER', 'IRON DAGGER', 'IRONDAGGER'):`; CPython even specifically optimizes this case to avoid building the `tuple` live on each pass by storing it as a constant that can be loaded all at once without construction. – ShadowRanger Mar 26 '16 at 15:48
  • @ShadowRanger ah yes, that is certainly more Pythonical way of doing it... – Ian Mar 26 '16 at 15:51