如何使if语句返回到代码

时间:2017-05-02 06:34:25

标签: python python-3.x

    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("What would you like to buy? ")
    print('')



    for text2 in shopitemsF:
        if shopchoice in text2:
            print(text2)

            if int(text2[-3:]) >= gold:
                print("You need another", int(text2[-3:]) - gold, "gold.")

            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.")
            elif int(text2[-3:]) <= gold:
                print("You have purchased,", text2[:-11]+".")
                x = (int(text2[-21:-18]))

我希望一旦代码到达if语句

    if int(text2[-3:]) >= gold:
       print("You need another", int(text2[-3:]) - gold, "gold.")

它会重新显示&#34;你想买什么? &#34;输入,以便他们能够继续选择一个项目,直到他们选择一个他们能负担得起的项目,并将导致elif语句运行。就像我想象的那样,我需要一个while循环,但由于这个代码实例,我不确定该怎么做。

1 个答案:

答案 0 :(得分:0)

我只会回答这个问题。我时间不够,因此无法对您的计划发布可能/必要的改进。也许其他答案会提供给他们。如果这部分程序按预期正常运行而没有错误,您可以将其发布到https://codereview.stackexchange.com以获取输入如何改进它。

您可以使用布尔变量repeat_shopping来控制while循环:

repeat_shopping=True
while repeat_shopping:
    for text2 in shopitemsF:
        if shopchoice in text2:
            print(text2)

            if int(text2[-3:]) >= gold:
                print("You need another", int(text2[-3:]) - gold, "gold.")

            elif int(text2[-3:]) <= gold:
                print("You have purchased,", text2[:-11]+".")
                x = (int(text2[-21:-18
                repeat_shopping=False


    for text2 in shopitemsM:
        if shopchoice in text2:
            print(text2)

            if int(text2[-3:]) >= gold:
                print("You need another", int(text2[-3:]) - gold, "gold.")
            elif int(text2[-3:]) <= gold:
                print("You have purchased,", text2[:-11]+".")
                x = (int(text2[-21:-18]))
                repeat_shopping=False

您甚至可以使用break语句

来避免此变量
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.")

            elif int(text2[-3:]) <= gold:
                print("You have purchased,", text2[:-11]+".")
                x = (int(text2[-21:-18
                break


    for text2 in shopitemsM:
        if shopchoice in text2:
            print(text2)

            if int(text2[-3:]) >= gold:
                print("You need another", int(text2[-3:]) - gold, "gold.")
            elif int(text2[-3:]) <= gold:
                print("You have purchased,", text2[:-11]+".")
                x = (int(text2[-21:-18]))
                break

但不是这些方法之间的区别。如果使用变量,则可以在设置变量后执行一些活动(例如,如果在第一个if语句中设置了变量,则输入第二个if语句)。如果使用break语句,则会立即保留while循环。