2017-05-03 10 views
0

このコードは参考用です。彼らは400ゴールドで始まり、アイテムが高すぎる場合、別のアイテムを選びたいかどうかを尋ねます。私の問題は、whileループを無限にループする余裕があるアイテムを選んだ場合、その理由がわからないことです。これはどうwhileループはelif文に達した場合に無限ループします

shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300", "Bloodcursed Sword: 160 Damage, Cost 800"] 
    shopitemsM = ["Fire Throw: 150 Damage, Cost: 700", "Ice Wind: 120 Damage, Cost: 300", "Electric shock: 160 Damage, Cost 800"] 


    print("Welcome to the shop.") 
    print('') 
    if character == "Fighter": 
     g = ', ' 
     print(g.join(shopitemsF)) 
     time.sleep(1) 
    elif character == "Mage": 
     g = ', ' 
     print(g.join(shopitemsM)) 
     time.sleep(1) 

    shopchoice = input("Please pick another item? ") 
    print('') 

    while True: 
     for text2 in shopitemsF: 
      if shopchoice in text2: 
       print(text2) 
       if int(text2[-3:]) >= gold: 
        print("You need another", int(text2[-3:]) - gold, "gold.") 
        shopchoice = input("Please pick another item? ") 
        break      
       elif int(text2[-3:]) <= gold: 
        print("You have purchased,", text2[:-11]+".") 
        x = (int(text2[-21:-18])) 


     for text2 in shopitemsM: 
      if shopchoice in text2: 
       print(text2) 
       if int(text2[-3:]) >= gold: 
        print("You need another", int(text2[-3:]) - gold, "gold.") 
        shopchoice = input("Please pick another item? ") 
        break 
       elif int(text2[-3:]) <= gold: 
        print("You have purchased,", text2[:-11]+".") 
        x = (int(text2[-21:-18])) 
+0

どのelifステートメント?いくつかの外側のループ状態があるように思えますが、これは 'if'では変更されますが、' elif'では変更されません。したがって、ループ状態が変わることはないので、elifに当たったら直ちに止まります。 –

答えて

1

:あなたは、変数foundを作成

shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300", "Bloodcursed Sword: 160 Damage, Cost 800"] 
shopitemsM = ["Fire Throw: 150 Damage, Cost: 700", "Ice Wind: 120 Damage, Cost: 300", "Electric shock: 160 Damage, Cost 800"] 


print("Welcome to the shop.") 
print('') 
if character == "Fighter": 
    g = ', ' 
    print(g.join(shopitemsF)) 
    time.sleep(1) 
elif character == "Mage": 
    g = ', ' 
    print(g.join(shopitemsM)) 
    time.sleep(1) 

shopchoice = input("Please pick another item? ") 
print('') 

found = False 
while found != True: 
    for text2 in shopitemsF: 
     if shopchoice in text2: 
      print(text2) 
      if int(text2[-3:]) >= gold: 
       print("You need another", int(text2[-3:]) - gold, "gold.") 
       shopchoice = input("Please pick another item? ") 
       break      
      elif int(text2[-3:]) <= gold: 
       print("You have purchased,", text2[:-11]+".") 
       x = (int(text2[-21:-18])) 
       found = True 


    for text2 in shopitemsM: 
     if shopchoice in text2: 
      print(text2) 
      if int(text2[-3:]) >= gold: 
       print("You need another", int(text2[-3:]) - gold, "gold.") 
       shopchoice = input("Please pick another item? ") 
       break 
      elif int(text2[-3:]) <= gold: 
       print("You have purchased,", text2[:-11]+".") 
       x = (int(text2[-21:-18])) 
       found = True 

はfalseに設定し、アイテムが偽それを維持します見つけない限り。その後、while found != True:は、アイテムが高価すぎるインスタンスのループを維持します。しかし、彼らが商品を購入した場合、見つかったものはtrueに設定され、ループから外れます

+0

私はちょっとだけ説明するために私の答えを更新しました。それがまだ不明な場合私に教えてください –

+0

非常に迅速な返信と理解していただきありがとうございます。うまく動作します – mykill456

+0

問題なく、うれしくて助かりました –

0

shopchoiceをもう一度尋ねることは決してありません。最初に選択したものは永遠に残っています。 shopchoice = input (...)ステートメントをwhileループの先頭に1回だけ入れてください。 )あなたは店から出る方法を考えなければならない;)

+0

ええ、私はそれがまだ働いているということを理解できない機能を私がまだ学んでいるから使わなければならないと言われました。とにかく返信ありがとう – mykill456

関連する問題