2016-03-26 8 views
-1

これは私のPython RPGゲームの店のコードです。どのような値を選んでも、私は短剣をタイプした場合のコードを実行します。私には不十分な資金があるとは決して言わない。/elif文が私に1つの答えしか与えない場合

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

または、よりPythonically:することになっている

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

:常に同じ答え

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() 

答えて

1

あなたOR条件を記述する方法が間違っている

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

Whあなたは何が起こることは

  1. あなたchoiceDAGGERある場合は、ないチェックを行うということですif choice == 'DAGGER' or 'IRON DAGGER'を行うエンことTrueですか
  2. もしあなたchoiceIRON DAGGERあることTrue

ですが

場合はチェック
  1. あなたのchoiceDAGGERあるIRON DAGGERif 'IRON DAGGER'は常にTrueを返すことTrue

ノートであればTrueまたは

  • です:

    if 'IRON DAGGER': #this is always true 
    
  • +0

    それともそれはPythonのだから、あなたは何度も変数名の繰り返しを避けることができます'' DAGGER '、' IRON DAGGER '、' IRONDAGGER ')の場合は '; CPythonは、このケースを特に最適化して、パスごとに 'tuple'ライブを構築することを回避します。これは、構築せずにすべてを一度にロードできる定数として格納することです。 – ShadowRanger

    +0

    @ShadowRangerああ、それは確かにそれを行うよりPythonの方法です... – Ian

    関連する問題