python - If/elif statement only gives me one answer -


this question has answer here:

this code shop in python rpg game. whatever value select, code executes things if typed dagger. never tells me have insufficient funds. same answer

global gcredits dagger = item('iron dagger', 5, 5) sword = item('iron sword', 10, 12) armour = item('iron armour', 15, 20)   print ("welcome shop! buy amour , 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 buy?').upper()  if choice == 'dagger' or 'iron dagger' or 'irondagger':     print ("you have selected 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 not item. try again.")     shop() 

the way write or condition wrong:

if choice == 'dagger' or 'iron dagger' or 'irondagger': 

it supposed be:

if choice == 'dagger' or choice == 'iron dagger' or choice == 'irondagger': 

or, more pythonically:

if choice in ('dagger', 'iron dagger', 'irondagger'): 

when if choice == 'dagger' or 'iron dagger' happen not check if

  1. your choice dagger true or if
  2. your choice iron dagger true

but check if

  1. your choice dagger true or
  2. if iron dagger true

note if 'iron dagger' return true:

if 'iron dagger': #this true 

Comments