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