Python code: how to return to the beginning of the program
SolvedPhil_1857 Posted messages 1883 Registration date Status Membre Last intervention -
Hello everyone, I’m a beginner in coding, and I need to do a small project (a shopping list).
My program is not fully working yet, before making all the necessary modifications, I would like to know how I can restart the program once the user has chosen an option. I believe I need to use While but I'm not sure how to integrate it into the program; when I run the program, it keeps asking for the chosen option infinitely but does not return to the beginning.
Thanks in advance for your help :)
#The list liste = [] # What the user sees print("Choose from the 5 options below") print("1: Add an item to the list") print("2: Remove an item from the list") print("3: Display the list") print("4: Clear the list") print("5: Quit") a = "1" "2" "3" "4" "5" a = input("Your choice: ") # loop to return to the beginning of the program while True: if a == "1": b = input("Enter the name of an item to add to your shopping list: ") liste.append(b) print(f"The item {b} has been successfully added to the list") elif a == "2": c = input("Remove an item from the list: ") if c not in liste: print("The item is not in my list") else: liste.remove(c) print(f"The item {c} has been successfully removed from the list") elif a == "3": print(liste) elif a == "4": liste.clear() print("The list has been cleared") elif a == "5": quit() else: print("Invalid entry!") a = input("Your choice: ")
6 réponses
Hello,
You need to incorporate your choice into your loop...
See you later blux "The idiots dare everything.
It's even how we recognize them."
Hello,
If I understand your problem correctly, you want your program to restart once the user has chosen an option?
The "while True" loop allows a program to restart indefinitely until the user chooses the option to quit. Try to integrate it higher up in your program before the selection of the 5 options.
Hello,
The loop must include all the instructions to be repeated. What are they?
Hello,
You include everything in the while loop, and you even add another one to control the input
(as long as the response is wrong, the menu is displayed again for a new input):
# -*- coding:Utf-8 -*- import os menu = ''' 1 Add 2 Remove 3 Display 4 Show 5 Quit > ''' while(True): choice = 0 while(not choice in [1,2,3,4,5]): os.system('cls' if os.name=='nt' else 'clear') print('\n' + '''Choose: '''.center(50,'*')) choice = int(input(menu)) if(choice == 1): ..... elif(choice == 5): break #exit the while: end of program
Thank you for your response, I am a beginner and I do not understand this part of the code, can you explain it to me?
Thank you :)
while(True): choix = 0 while(not choix in [1,2,3,4,5]): os.system('cls' if os.name=='nt' else 'clear') print('\n' + '''Choose:'''.center(50,'*')) choix = int(input(menu))
My goal is for the user to return to the beginning of the program at ( a = input "your choice: ") after doing what they wanted to do (such as adding an item to the shopping list). This way, they can see that they have indeed added the item to the list.
I'm not sure if I should actually use while True all the time when I start my program, and when I select 1 and add an item, it returns me to (" b = input("Enter the name of an item to add to your shopping list: "))
Explanation of my code:
The outer while loop (while(True)) means that after choosing 1, 2, 3 or 4 from the menu and executing the corresponding code, we go back to the menu for a new choice.
If we choose 5, we hit my break statement which makes us exit this while loop and the program stops.
.
The inner while loop (while(not choice .........)) means that if we enter anything other than 1, 2, 3, 4 or 5, the screen clears
and we redisplay the menu until we enter the correct number.
If it's okay, we exit this while loop and continue with the if tests (if(choice == 1), etc...)
choice = 0 while(not choice in [1,2,3,4,5]): # at first choice = 0, so we display the menu os.system('cls' if os.name=='nt' else 'clear') #clear the screen print('\n' + '''Choose :'''.center(50,'*')) choice = int(input(menu)) #display menu to enter a value # as long as choice is different from 1,2,3,4 or 5, we loop again # otherwise, we exit this while
Thank you for your help, indeed, that was the idea I had in mind at the beginning, to create a while loop if the user does not enter the correct value and make it return to the beginning at the end of its correct application.
One last quick question, I would like to know the meaning of the commands:
os.name=='nt' and center(50,'*'))
Thank you again for your help.
os is the module that handles everything related to the os, the operating system,
the operating system (Windows for me)
and therefore os.name gives the name of your os
os.system('cls') allows you to clear the screen, but I added a test to make the command
more universal: if it's Windows, we take 'cls', otherwise we take 'clear'
.
center(50,*) is a display format that frames the text to be displayed on a total of 50 characters,
framed by *, as you can see in the image I displayed above
Hello,
In the end, it would look like this (I corrected one or two mistakes):
# -*- coding:Utf-8 -*- import os menu = ''' 1 Add 2 Remove 3 Display 4 Clear 5 Quit > ''' list = [] while(True): choice = 0 while(not choice in [1,2,3,4,5]): # os.system('cls' if os.name=='nt' else 'clear') print('\n' + '''Choose: '''.center(50,'*')) choice = int(input(menu)) if(choice == 1): el = input("Enter the name of an item to add to your shopping list: ") list.append(el) print(f"The item {el} has been successfully added to the list.") elif(choice == 2): el = input("Item to remove from the list: ") if el not in list: print("The item is not in my list.") else: list.remove(el) print(f"The item {el} has been successfully removed from the list.") elif(choice == 3): print(list) elif(choice == 4): list.clear() elif(choice == 5): break