简体   繁体   中英

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

I would like it so once the code reaches the if statement

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

It would re-display the "What would you like to buy? " input so that they are able to keep choosing an item until they pick an item that they can afford and will cause the elif statement to run. Like I imagine I would need a while loop but because of this instance of code I am unsure on what to do.

I will only answer the question. I am short of time and therefore cannot post possible/necessary improvements to your program. Maybe other answers will provide them. If this part of program is working as expected without errors you can post it on https://codereview.stackexchange.com to get inputs how to improve it.

You can use a boolean variable repeat_shopping to control a while loop:

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

You can even avoid this variable by using a break statement

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

But not the difference between these approaches. If you use the variable, you can do some activities after setting the variables (eg entering the second if statement if the variable is set in the first if statement). If you use the break statement the while loop will be left immediately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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