oklol
oklol

Reputation: 11

how do i make a while loop based on input?

im trying to make a thing where the user starts off in a main menu, inputs a command, and then returns to the main menu and repeats until they finish. so i put this:

#mainmenu loop
def main():
    while True:
        print("welcome to the store. what do you wanna do?"
              "\n here are all the commands. enter a number to output. enjoy!"
              "\n 1: view your shopping list"
              "\n 2: add to the shopping list"
              "\n 3: delete items from the shopping list"
              "\n 4: clear the shopping list"
              "\n 5: go to isle 1"
              "\n 6: go to isle 2"
              "\n 7: go to isle 3"
              "\n 8: go to isle 4"
              "\n 9: checkout"
              "\n 10: leave the store")
        selected = str(input('>> '))

and then i have the rest of the functions and stuff later on.

if selected == '1':
    viewList()

but it only repeats twice then stops, and it also has no response when i enter a command. i tried moving where i said main() but that didn't work.

full code:

import sys

#mainmenu loop
def main():
    while True:
        print("welcome to the store. what do you wanna do?"
              "\n here are all the commands. enter a number to output. enjoy!"
              "\n 1: view your shopping list"
              "\n 2: add to the shopping list"
              "\n 3: delete items from the shopping list"
              "\n 4: clear the shopping list"
              "\n 5: go to isle 1"
              "\n 6: go to isle 2"
              "\n 7: go to isle 3"
              "\n 8: go to isle 4"
              "\n 9: checkout"
              "\n 10: leave the store")
        selected = str(input('>> '))

main()

selected = main() 

#shopping list
list = ['']

#functions for each command

#view list
def viewList():
    if list == '':
        print('the list is empty.')
    else:
        print(list)

#add
def addList(item):
    item = input("what would you like to add? ")
    list.append(item)
    print(item, ' has been added.')

#delete
def removeList(item):
    item = input("what would you like to remove? ")
    list.remove(item)
    print(item, ' has been deleted.')

#if/else statements for selection
if selected == '1':
    viewList()
elif selected == '2':
    addList(item)
elif selected == '3':
    pass
elif selected == '4':
    pass
elif selected == '5':
    pass
elif selected == '6':
    pass
elif selected == '7':
    pass
elif selected == '8':
    pass
elif selected == '9':
    pass
elif selected == '10':
    print('thanks for visiting :)')
    sys.exit() 
else:
    print('invalid code..')

Upvotes: 1

Views: 568

Answers (3)

Sridhar T
Sridhar T

Reputation: 11

I worked on your code. I hope this is your expected outcome at this time. I have also fixed some bugs. I have one suggestion to you, please revisit the flow inside code, as the main menu would be repeating again which many would not prefer right after updating shopping list.

Modified code below:

    #mainmenu loop
def main():
optcmd = 0
print("\n\nwelcome to the store. what do you wanna do?"
    "\n here are all the commands. enter a number to output. enjoy!"
    "\n 1: view your shopping list"
    "\n 2: add to the shopping list"
    "\n 3: delete items from the shopping list"
    "\n 4: clear the shopping list"
    "\n 5: go to isle 1"
    "\n 6: go to isle 2"
    "\n 7: go to isle 3"
    "\n 8: go to isle 4"
    "\n 9: checkout"
    "\n 10: leave the store")
while not optcmd:
    optcmd = int(input('>> '))
    if optcmd<=0 or optcmd>10:
    print('invalid code...\nPlease re-enter code...')
    optcmd=0
return optcmd

#functions for each command
#view list
def viewList(shoplist):
if shoplist==[]:
    print('the list is empty.')
else:
    print(shoplist)

#add
def addList(shoplist):
item = input("what would you like to add? ")
shoplist.append(item)
print(item, ' has been added.')

#delete
def removeList(shoplist):
item = input("what would you like to remove? ")
shoplist.remove(item)
print(item, ' has been deleted.')

#shopping list
shoplist = []
selected = main()
#selection item 
while selected:
#if/else statements for selection
if selected == 1:
    viewList(shoplist)
    selected = main()
elif selected == 2:
    addList(shoplist)
    selected = main()
elif selected == 3:
    removeList(shoplist)
    selected = main()
elif selected == 4:
    pass
    selected=''
elif selected == 5:
    pass
    selected=''
elif selected == 6:
    pass
    selected=''
elif selected == 7:
    pass
    selected=''
elif selected == 8:
    pass
    selected=''
elif selected == 9:
    pass
    selected=''
elif selected == 10:
    print('thanks for visiting :)')
    selected=''

Upvotes: 1

Raghav Agarwal
Raghav Agarwal

Reputation: 57

There are quite bugs in your code: 1.your main function should return a value so that selected can store the value from main() function and all the if conditions should come inside the while loop for your working,i m giving you the full code,please correct the syntax errors.

and i m guessing if user inputs 10 he should exit so add that condition too.

list = ['']




def viewList():
if list == '':
    print('the list is empty.')
else:
    print(list)


def addList(item):
    item = input("what would you like to add? ")
    list.append(item)
    print(item, ' has been added.')


def removeList(item):
    item = input("what would you like to remove? ")
    list.remove(item)
    print(item, ' has been deleted.')



def main():
    flag = True
    while flag:
        print("welcome to the store. what do you wanna do?"
              "\n here are all the commands. enter a number to output. enjoy!"
              "\n 1: view your shopping list"
              "\n 2: add to the shopping list"
              "\n 3: delete items from the shopping list"
              "\n 4: clear the shopping list"
              "\n 5: go to isle 1"
              "\n 6: go to isle 2"
              "\n 7: go to isle 3"
              "\n 8: go to isle 4"
              "\n 9: checkout"
              "\n 10: leave the store")
          
        selected = str(input('>> '))
        if(selected == 10):
            flag = False
        
    
        if selected == '1':
            viewList()
        elif selected == '2':
            addList(item)
        elif selected == '3':
            pass
        elif selected == '4':
            pass
        elif selected == '5':
            pass
        elif selected == '6':
            pass
        elif selected == '7':
            pass
        elif selected == '8':
            pass
        elif selected == '9':
            pass
        elif selected == '10':
            print('thanks for visiting :)')
        else:
            print("Invalid Code")

    
    
    
main()

Upvotes: 0

aahnik
aahnik

Reputation: 1881

while(True) is a good way to go. It will create an infinite loop. You must break out of it when user enters 10 ie leave the store

Can you share your full code?

You said it is stopping after two times, which may be due to some other portion of your code.

Have you called your method main ?

Upvotes: 0

Related Questions