繁体   English   中英

如何使if语句返回到代码中的先前输入

[英]How to make an if statement go back to the previous input in code

    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.")

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

我只会回答这个问题。 我的时间很短,因此无法发布对程序的可能/必要的改进。 也许其他答案会为他们提供。 如果程序的这一部分正常运行且没有错误,则可以将其发布在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循环将立即保留。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM