while循环无限循环,如果它到达elif语句

时间:2017-05-03 23:24:41

标签: python python-3.x

该代码仅供参考。他们从400金开始,如果他们选择一个太昂贵的项目,它会询问他们是否愿意选择另一个。我的问题是,如果他们选择一个他们可以承受while循环无限循环的项目,我不知道为什么。

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

2 个答案:

答案 0 :(得分: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将其设置为false,除非您找到该项,否则将其设为false。然后while found != True:让你的循环继续进行项目太昂贵的实例。但是,如果他们购买了该物品,则将其设置为true,这会使您脱离循环

答案 1 :(得分:0)

你再也不会要求<table id="headerAnimation"> <!--[if !mso]><!-- --> <tbody> <tr> <td width="600" height="600" align="left" valign="top" style="background:url('/pathtoimage.jpg'); width: 600px;max-width: 600px; height: 594px; min-height: 600px;"> <table> <tr> <img align="center" valign="bottom" id="aniGif" src="/pathtoimage.gif" alt="Campfire season is back" width="140" style="width: 22%;max-width: 22%; position:relative; top: 470px; left:39%;" /> </tr> </table> </td> </tr> </tbody> <!--<![endif]--> <!--[if mso]> <img src="/pathtoimage.jpg" alt="" style="width: 100%;max-width: 600px; height: 594px; min-height: 600px;" /> <img align="center" valign="bottom" id="aniGif" src="/pathtoimage.gif" alt="Campfire season is back" width="140" style="width: 22%;max-width: 22%; position:relative; margin-top: -125px; left:39%;" /> <![endif]--> </table> 了,所以你会被一开始就被选中的东西所困扰。只需在while循环的开头放置shopchoice语句一次。你还应该考虑一种让用户离开商店的方法;)