2017-05-03 195 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

這個怎麼樣:

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 

您創建一個變量found將其設置爲假,除非你找到該項目保持假。然後while found != True:讓您的循環繼續處理物品太貴的情況。但是如果他們購買了物品,發現設置爲true,這會讓你脫離環路

+0

我剛剛更新了我的答案,以更多地解釋它。讓我知道如果它仍然不清楚 –

+0

非常感謝你的超快速答覆和易於理解。很好的 – mykill456

+0

不是問題,很高興它幫助 –

0

你永遠不會再要求shopchoice,所以你永遠不會再被選中。只需在while循環的開始處將您的shopchoice = input (...)語句放入一次即可。你也應該想辦法讓用戶走出店鋪;)

+0

是的我已經被告知我需要使用一個函數,我不明白它如何工作,因爲我仍然在學習。無論如何感謝您的回覆 – mykill456