How to put this in a loop

Question:

I need to put this code in a loop so that you can choose whichever number first and go back to the start after whichever one you choose, but everything I’ve tried hasn’t worked and need help.

peoples = {
"Mary": {
    "name": "Mary",
    "budget": 100,
    "items": {
        "Game": 0,
        "Book": 0,
        "Kindle": 0
    },
    "status": "incomplete"
},
"Steve": {
    "name": "Steve",
    "budget": 100,
    "items": {
        "Tie": 0,
        "Scarf": 0,
        "Amazon Echo": 0
    },
    "status": "incomplete"
},
"Kevin": {
    "name": "Kevin",
    "budget": 65,
    "items": {
        "Mario Kart": 0
    },
    "status": "incomplete"
},
"Jane": {
    "name": "Jane",
    "budget": 50,
    "items": {
        "Gift Card": 0,
        "Gloves": 0
    },
    "status": "incomplete"
},
"Chris": {
    "name": "Chris",
    "budget": 100,
    "items": {
        "Chocolates": 0,
        "Galaxy Tab": 0
    },
    "status": "incomplete"
}
}

print("""
Menu
--------------------
1. Update Shopping List
2. Complete Shopping List
3. Display Shopping List
4. Exit Application
--------------------
Make your selection

""")

option = int(input("Enter an option: "))

if option == 1:
    people = input("Who are you updating?: ")
    print("nCurrent values of people",people)
    print(peoples[people])
    print("nAvailable items and their prices are:")
    for item in peoples[people]["items"]:
        print(item, peoples[people]["items"][item])
    item_to_update = input("Enter an item to update: ")
    price = int(input("Enter updated price: "))
    budget = peoples[people]["budget"] - peoples[people]["items"] 
[item_to_update] - price
peoples[people]["items"][item_to_update] = price
peoples[people]["budget"] = budget
print("nUpdated values of people",people)
print(peoples[people])

option = int(input("nEnter an option: "))

if option == 2:
    update = input("Choose one of the 5 people to complete their shopping list: ")
if update in peoples:
        print("You have chosen",update)
answer = input("Do you want to complete their shopping list (Y/N)? ")
if answer.upper() == "Y":
    peoples[people]['status'] = 'complete'
print("Shopping list has been completed!")

option = int(input("nEnter an option: "))

if option == 3:
    display = input("Who's do you want to look at?: ")
print("nShopping List Of",display)
print(peoples[display])

option = int(input("nEnter an option: "))

if option == 4:
    print("Thank You For Shopping With Us!")

I’ve tried putting in different versions of loop, but it always either results in the program ignoring it and not going back to the start, or breaking when I choose something else then 1 at the start.

option = input("Enter an option: ")
if option == "1":
        people = input("nWho are you updating?: ")
print("nCurrent values of people",people)
print(peoples[people])
print("nAvailable items and their prices are:")
for item in peoples[people]["items"]:
    print(item, peoples[people]["items"][item])
item_to_update = input("Enter an item to update: ")
price = int(input("Enter updated price: "))
budget = peoples[people]["budget"] - peoples[people]["items"][item_to_update] - price
peoples[people]["items"][item_to_update] = price
peoples[people]["budget"] = budget
print("nUpdated values of people",people)
print(peoples[people])

elif option == "2":
        update = input("Choose one of the 5 people to complete their shopping list: ")
        if update in peoples:
print("You have chosen",update)
peoples[people]['status'] = 'complete'
print("Shopping list has been completed!")

elif option == "3":
    display = input("Who's do you want to look at?: ")
print("nShopping List Of",display)
print(peoples[display])

elif option == "4":
    print("Thank You For Shopping With Us!")
    break
else:
    print("That's not a valid answer! Try again!")

With the same list above, After adding in my information to the set example given, I would get the error below.

error with pic: https://i.stack.imgur.com/BrqBB.png

peoples = {
    "Mary": {
        "name": "Mary",
        "budget": 100,
        "items": {
            "Game": 0,
            "Book": 0,
            "Kindle": 0
        },
        "status": "incomplete"
    },
    "Steve": {
        "name": "Steve",
        "budget": 100,
        "items": {
            "Tie": 0,
            "Scarf": 0,
            "Amazon Echo": 0
        },
        "status": "incomplete"
    },
    "Kevin": {
        "name": "Kevin",
        "budget": 65,
        "items": {
            "Mario Kart": 0
        },
        "status": "incomplete"
    },
    "Jane": {
        "name": "Jane",
        "budget": 50,
        "items": {
            "Gift Card": 0,
            "Gloves": 0
        },
        "status": "incomplete"
    },
    "Chris": {
        "name": "Chris",
        "budget": 100,
        "items": {
            "Chocolates": 0,
            "Galaxy Tab": 0
        },
        "status": "incomplete"
    }
}

print("""
Menu
--------------------
1. Update Shopping List
2. Complete Shopping List
3. Display Shopping List
4. Exit Application
--------------------
Make your selection

""")

while True:
    option = input("Enter an option: ")
    if option == "1":
        people = input("nWho are you updating?: ")
        print("nCurrent values of people",people)
        print(peoples[people])
        print("nAvailable items and their prices are:")
    for item in peoples[people]["items"]:
        print(item, peoples[people]["items"][item])
        item_to_update = input("Enter an item to update: ")
        price = int(input("Enter updated price: "))
        budget = peoples[people]["budget"] - peoples[people]["items"][item_to_update] - price
        peoples[people]["items"][item_to_update] = price
        peoples[people]["budget"] = budget
        print("nUpdated values of people",people)
        print(peoples[people])
    
    elif option == "2":
        update = input("Choose one of the 5 people to complete their shopping list: ")
        if update in peoples:
            print("You have chosen",update)
            peoples[people]['status'] = 'complete'
            print("Shopping list has been completed!")
    
    elif option == "3":
        display = input("Who's do you want to look at?: ")
        print("nShopping List Of",display)
        print(peoples[display])
    
    elif option == "4":
        print("Thank You For Shopping With Us!")
        break
    else:
        print("That's not a valid answer! Try again!")

It now looks exactly like this, and it still gives back a syntax error on the first elif statement. I don’t understand what the problem is if it’s properly indented and should follow the correct rules to use it.

edited with error: https://i.stack.imgur.com/rTW6k.png

The syntax error is finally gone, but now lies the problem where the code just repeats itself on the menu screen without going anywhere, like this:

repeating: https://i.stack.imgur.com/YNPdF.png

Asked By: john man

||

Answers:

I would do something like this:

while True:
    print(<instructions>)
    option = input("Enter an option: ")
    if option == "1":
        do stuff...
    elif option == "2":
        do number two stuff..
    elif option == "3":
        do that third stuff..
    elif option == "4":
        print("Thank You For Shopping With Us!")
        break
    else:
        print("That's not a valid answer! Try again!")

This will keep the menu in a loop and if option 4 is selected, it will break from the loop and continue on.

The issue now is with your indentation. You must indent your code properly for python to be able to understand what you want it to do, for instance:

x= "3"
if x == "2":
    print("hello world")
print("outside the indent")

you console output would be:

>>outside the indent

but if your code looks like this:

x= "3"
if x == "2":
    print("hello world")
    print("outside the indent")

you would get no output from the console, everything is within the "if" code block. Indentation is crucial for python to exhibit the expected behavior. you need to make sure that all your code for each condition is indented properly inside the if blocks, like the example I gave above. Also, if you want this in a loop, you need to put it in a loop with the while True: statement, and indent everything inside it.

Your final result should look something like this:

peoples = {
    "Mary": {
        "name": "Mary",
        "budget": 100,
        "items": {
            "Game": 0,
            "Book": 0,
            "Kindle": 0
        },
        "status": "incomplete"
    },
    "Steve": {
        "name": "Steve",
        "budget": 100,
        "items": {
            "Tie": 0,
            "Scarf": 0,
            "Amazon Echo": 0
        },
        "status": "incomplete"
    },
    "Kevin": {
        "name": "Kevin",
        "budget": 65,
        "items": {
            "Mario Kart": 0
        },
        "status": "incomplete"
    },
    "Jane": {
        "name": "Jane",
        "budget": 50,
        "items": {
            "Gift Card": 0,
            "Gloves": 0
        },
        "status": "incomplete"
    },
    "Chris": {
        "name": "Chris",
        "budget": 100,
        "items": {
            "Chocolates": 0,
            "Galaxy Tab": 0
        },
        "status": "incomplete"
    }
}

print("""
Menu
--------------------
1. Update Shopping List
2. Complete Shopping List
3. Display Shopping List
4. Exit Application
--------------------
Make your selection

""")

while True:
    option = input("Enter an option: ")
    if option == "1":
        people = input("nWho are you updating?: ")
        print("nCurrent values of people",people)
        print(peoples[people])
        print("nAvailable items and their prices are:")
        for item in peoples[people]["items"]:
            print(item, peoples[people]["items"][item])
            item_to_update = input("Enter an item to update: ")
            price = int(input("Enter updated price: "))
            budget = peoples[people]["budget"] - peoples[people]["items"][item_to_update] - price
            peoples[people]["items"][item_to_update] = price
            peoples[people]["budget"] = budget
            print("nUpdated values of people",people)
            print(peoples[people])

    elif option == "2":
        update = input("Choose one of the 5 people to complete their shopping list: ")
        if update in peoples:
            print("You have chosen",update)
            peoples[people]['status'] = 'complete'
            print("Shopping list has been completed!")

    elif option == "3":
        display = input("Who's do you want to look at?: ")
        print("nShopping List Of",display)
        print(peoples[display])

    elif option == "4":
        print("Thank You For Shopping With Us!")
        break
    else:
        print("That's not a valid answer! Try again!")

Also, please review this link as it is crucial you understand how to properly indent your code when writing python.

https://www.geeksforgeeks.org/indentation-in-python/

Answered By: Sean Palmer