mykill456
mykill456

Reputation: 91

while loop infinitely loops if it reaches the elif statement

The code is for a shop just for a reference. They start with 400 gold and if they pick an item too expensive it asks if they would like to pick another. My issue is if they pick an item that they can afford the while loop infinitely loops which I am not sure why.

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

Upvotes: 0

Views: 179

Answers (2)

Anis Jonischkeit
Anis Jonischkeit

Reputation: 894

how about this:

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

You create a variable found set it to false, and unless you find the item keep it false. Then while found != True: keeps your loop going for the instance where the item is too expensive. If however they bought the item, found is set to true which breaks you out of the loop

Upvotes: 1

kosiokarchev
kosiokarchev

Reputation: 96

You never ask for a shopchoice again, so you are stuck with whatever was chosen in the beginning forever. Just put your shopchoice = input (...) statement only once in the beginning of the while loop. You should also think of a way of letting the user out of the shop ;)

Upvotes: 0

Related Questions