Shell is not showing text from my prints - python

I'm trying to create a code for my lists work and i have written this at the moment:
list = [ ]
def Menu(choice):
print("""Menu:
1. View entire list
2. View one item
3. Reset list
4. Edit list
5. Quit
""")
choice = int(input("Please choose an option: "))
while True:
if choice == "1":
print (list)
Menu()
elif choice == "2":
oneItem = int(input('Please enter an item to view (1-5): '))
oneItem = oneItem - 1
oneView = list[(oneView)]
print (oneView)
Menu()
elif choice == "3":
list = [None]*6
Menu()
elif choice == "4":
def carryon():
for i in range(6):
add = input("Please enter a new item for the list: ")
list.append(add)
carryOn = input("Would you like to carry on? (y/n): ")
if carryOn == "y":
carryon()
else:
break
break
Menu()
When I run the code in shell the menu appears fine:
Menu:
1. View entire list
2. View one item
3. Reset list
4. Edit list
5. Quit
Please choose an option: 3
But then it just comes up blank. I can type things in still but nothing happens. Even after i press enter it just carries on. I use python 3.4 (gui)

Related

Python Shopping Cart with only outer while loop

I'm new to python and programming in general.
I'm starting with my first mini project, a shopping cart.
I have everything working but I have been told I could get the whole loop going with just the outer one and that I do not require the second one. I've been wracking my brain all day trying to see how to get it to work, to no avail. Some pointers of how it can be achieved would be very much appreciated. Thank you.
shopping_list = []
print("Hi, Welcome to Jolly's Market.")
while True:
customer = input("To add to the shopping cart, press 1. To checkout and leave press 2.\n")
if customer == "1":
print("To return to the menu, type exit . To remove items, type r")
while customer != "exit" or customer != "r":
shopping_list.append(input("Add to cart: "))
print(shopping_list)
customer = input("").lower()
if customer == "exit":
print("Sending you back to the menu")
break
if customer == "r":
shopping_list.pop(int(input("Remove item ")))
print(shopping_list)
shopping_list.append(input("Add to cart: "))
print(shopping_list)
customer = input("").lower()
if len(shopping_list) == 10:
print("You have ten items, do you wish to add more? (y, n)")
customer = input(" ").lower()
if customer == "y":
shopping_list.append(input("Add to cart: "))
elif customer == "n":
print("Sending you back to the main menu")
break
The inner while loop is not required because it can be done with only the outer while loop.
Here's how you can modify the code:
shopping_list = []
print("Hi, Welcome to Jolly's Market.")
while True:
customer = input("To add to the shopping cart, press 1. To checkout and leave press 2.\n")
if customer == "1":
print("To return to the menu, type exit . To remove items, type r")
item = input("Add to cart: ")
shopping_list.append(item)
print(shopping_list)
customer = input("").lower()
if customer == "exit":
print("Sending you back to the menu")
continue
if customer == "r":
shopping_list.pop(int(input("Remove item ")))
print(shopping_list)
item = input("Add to cart: ")
shopping_list.append(item)
print(shopping_list)
customer = input("").lower()
if len(shopping_list) == 10:
print("You have ten items, do you wish to add more? (y, n)")
customer = input(" ").lower()
if customer == "y":
item = input("Add to cart: ")
shopping_list.append(item)
elif customer == "n":
print("Sending you back to the main menu")
continue
elif customer == "2":
break
Here, the inner while loop is not required because the menu options are already in the outer loop. When the user types 1, the shopping list is updated, and then the user is asked to either add or remove items. If the length of the shopping list reaches 10, the user is asked if they want to add more. If the user types exit or n, they are sent back to the main menu. If the user types 2, the while loop breaks and the program ends.
Here's a revised version of the code that will let you continuously add items to the shopping list without being taken back to the main menu after each item, until you reach 10 items or decide to exit:
shopping_list = []
print("Hi, Welcome to Jolly's Market.")
while True:
customer = input("To add to the shopping cart, press 1. To checkout and leave press 2.\n")
if customer == "1":
print("To return to the menu, type exit . To remove items, type r")
while True:
item = input("Add to cart: ")
shopping_list.append(item)
print(shopping_list)
if len(shopping_list) == 10:
print("You have ten items, do you wish to add more? (y, n)")
customer = input(" ").lower()
if customer == "n":
break
else:
continue
customer = input("").lower()
if customer == "exit":
print("Sending you back to the menu")
break
if customer == "r":
shopping_list.pop(int(input("Remove item ")))
print(shopping_list)
continue
elif customer == "2":
break

Menus and submenus in Python

I am a complete beginner in learning Python. Currently working on an assignment and having issues in creating menu with submenus.I am trying to connect functions properly and make my program work.
How can I make my submenu work? Output doesnt show the submenu options.
type def display_header():
main = "Main Menu"
txt = main.center(90, ' ')
print('{:s}'.format('\u0332'.join(txt)))
print("Please choose an option from the following menu:")
print("I. Invitee's Information")
print("F. Food Menu")
print("D. Drinks Menu")
print("P. Party Items Menu")
print("Q. Exit")
def get_user_choice():
choice = input("Enter your choice: ")
return choice
def invitees_menu():
invitees_menu()
while True:
choice = invitees_menu()
if choice == "a":
enter_invitee()
if choice == "e":
edit_invitee()
if choice == "v":
drinks_menu()
if choice == "b":
display_header()
print("Invitees' Information Menu")
print("Please choose an option from the following menu:")
print("A. Add new invitee information")
print("E. Edit existing invitee information")
print("V. View all invitees")
print("B. Go back to main menu")
choice = input("Enter your sub-menu choice: ")[0].lower
return choice
if __name__ == "__main__":
display_header()
while True:
choice = get_user_choice()
if choice == "i":
invitees_menu()
if choice == "f":
food_menu()
if choice == "d":
drinks_menu()
if choice == "p":
party_menu()
if choice == "q":
print ("Thank you for using the program!")
break
I might be wrong in understanding the code, but here's what I think's happened.
when you call your invitees_menu function it immediately calls itself. This breaks a rule of recursion (a function calling itself) and causes an infinite loop of just calling the start of the function over and over again. Which gives us this error:
RecursionError: maximum recursion depth exceeded
So first i'd remove the first invitees_menu line completely. Then in the while loop you're calling the invitees_menu again. This is again the same problem because each time you call the function, it will call itself again and never get to returning any item. Here i've replaced it with:
print("Invitees' Information Menu")
print("Please choose an option from the following menu:")
print("A. Add new invitee information")
print("E. Edit existing invitee information")
print("V. View all invitees")
print("B. Go back to main menu")
choice = input("Enter your sub-menu choice: ")[0].lower
You then have the problem of never actually leaving the while True loop. Since entering B should break out and then go back to the main loop in the __main__ call, so i replaced the display_header with break.
Finally, the last few smaller things are:
removing the "type" at line 1
moving the display header in main inside the while loop
fixing up the irregular tab structure in invitees_menu
And here it is
def display_header():
main = "Main Menu"
txt = main.center(90, ' ')
print('{:s}'.format('\u0332'.join(txt)))
print("Please choose an option from the following menu:")
print("I. Invitee's Information")
print("F. Food Menu")
print("D. Drinks Menu")
print("P. Party Items Menu")
print("Q. Exit")
def get_user_choice():
choice = input("Enter your choice: ")
return choice
def invitees_menu():
while True:
print("Invitees' Information Menu")
print("Please choose an option from the following menu:")
print("A. Add new invitee information")
print("E. Edit existing invitee information")
print("V. View all invitees")
print("B. Go back to main menu")
choice = input("Enter your sub-menu choice: ")[0].lower()
if choice == "a":
enter_invitee()
if choice == "e":
edit_invitee()
if choice == "v":
drinks_menu()
if choice == "b":
break
return choice
if __name__ == "__main__":
display_header()
while True:
choice = get_user_choice()
if choice == "i":
invitees_menu()
if choice == "f":
food_menu()
if choice == "d":
drinks_menu()
if choice == "p":
party_menu()
if choice == "q":
print ("Thank you for using the program!")
break
I suggest as first step to clean up the mess with recursive function calls in following section:
def invitees_menu():
invitees_menu()
while True:
choice = invitees_menu()
Next step in cleaning up the mess would be to remove the entire unnecessary parts of the code preventing the sub-menu from showing up when calling invitees_menu().

Insert input into list by the name of the menu, and then calculating its price after selecting the items

I wanted a create a function in Python where the user inputs the name of the menu and then it returns it in their order. After they are finished with ordering, the function would then calculate the price. My problem is I typed "Apple", but it came back empty. Is there anyway I could get around this? Any assistance is appreciated.
Here is the function:
menu = [{"Menu":"Apple","Price":9.00},{"Menu":"Banana","Price":5.00}], my_order = [], userInput = 0
try:
userInput = input("Enter item menu name that you want to select >> ") except ValueError:
print("Item does not exist.") if userInput in menu:
print("The item is in the list") else:
print("The item is not in the list. Please choose a different item.") while userInput != "Stop" or userInput != "stop":
print(f"Available menu: {menu}")
userInput = input("Do you want to add the item from the menu? If so please type appropriate item menu name. If no please type Stop. >> ")
if userInput == "Stop" or userInput == "stop":
print("The program has ended no more items will be added.")
print(f"Your order: {my_order}")
break
elif userInput not in menu:
print("Item does not exist in the list, try another item.")
print(f"Your order: {my_order}")
continue
else:
menu["Menu"] = userInput
my_order.append(userInput)
print(f"Your order: {my_order}")
continue
The way you use a dictionary is to have a key and a value related to it, in this case your menu dictionary should be {"item":price}.
That way if you want to know the price of an Apple yo do
print(menu["Apple"])
Did not understand why the Try/except in this case.
menu = {"Apple" : 9.00, "Banana" : 5.00}
my_order = []
userInput = ""
while userInput != "Stop" or userInput != "stop":
print(f"Available menu: {menu}")
userInput = input("Do you want to add the item from the menu? If so please type appropriate item menu name. If no please type Stop. >> ")
if userInput == "Stop" or userInput == "stop":
print("The program has ended no more items will be added.")
print(f"Your order: {my_order}")
break
elif userInput not in menu:
print("Item does not exist in the list, try another item.")
print(f"Your order: {my_order}")
continue
else:
my_order.append(userInput)
print(f"Your order: {my_order}")
continue
I let you continue calculating the total for the bill.

Python 3 Menu with Quit option

I'm trying to create a sort of CLI menu in Python (very new to this) and having an issue with the quit option mostly, it won't actually quit and jumps to the "Oooops that isn't right" option instead, or repeats the last step. It does seem to work if you put it as the first choice though
I know I must be doing something daft. I've tried just putting the variable at the end of the function, as well as the menu function itself but that didn't seem to work.
Snippet below if anyone can point me in the right direction.
def my_menu():
choice = input("Please choose an choice: ")
choice = choice.lower()
while (choice != "quit"):
if choice == "b":
a_thing()
my_menu()
if choice == "quit":
break
else:
print("Oooops that isn't right")
my_menu()
def a_thing():
print("a thing")
my_menu()
Try to input the choice another time at the end of the loop, remove the call to the my_menu() function and remove the if choice=="quit" block (because the loop will automatically quit when the choice is set to "quit")
def my_menu():
choice = input("Please choose an choice: ").lower()
while (choice != "quit"):
if choice == "b":
a_thing()
else:
print("Oooops that isn't right")
choice = input("Please choose an choice: ").lower()
def a_thing():
print("a thing")
my_menu()
Or you can remove the loop and just verify using if statements and in the case of "quit" you just put return to break the loop
def my_menu():
choice = input("Please choose an choice: ").lower()
if choice == "b":
a_thing()
elif choice == "quit":
return
else:
print("Oooops that isn't right")
my_menu()
def a_thing():
print("a thing")
my_menu()
I ran your code, and on its first iteration it runs as expected. After that, the recursive call to my_menu() starts to cause problems.
Walking through it, first you enter some random string, "hi", and it will enter the while loop and use the else case. This will call my_menu(), which then calls another while loop. When you enter that new while loop, any exiting that you do (e.g. break) won't exit the first loop, only the loop that your currently in, so you're in an infinite loop because you can never "go back" and change the value of choice in the first loop.
A way you could achieve this behavior with the least amount of changes to your code would be like this:
def my_menu():
choice = ""
while (choice != "quit"):
choice = input("Please choose an choice: ")
choice = choice.lower()
if choice == "b":
a_thing()
if choice == "quit":
break
else:
print("Oooops that isn't right")
def a_thing():
print("a thing")
my_menu()
(I removed your recursive calls to my_menu(), moved the input lines to within the loop, and initialized choice before the loop)

How would I go about creating an input input that restarts the code from the users end?

I am looking for a way to include an input at the end of this code where the user will be prompted with a choice to restart the code or end the code without having to manually restart it.
def correct_result(choice,num):
if choice.lower() == 'square': #Prints the Square of a number Ex. 2^2 = 4
return num**2
elif choice.lower() == 'sqrt': #Prints the Square root of a number Ex. √4 = 2
return num**.5
elif choice.lower() == 'reverse': #Changes the sign of the number
return(-num)
else:
return 'Invalid Choice' #prints an error message
choice = input() #Creates a answer box to enter the desired choice
num = int(input()) #Creates a second box that lets the user enter their number
print(correct_result(choice,num)) #Prints either the desired choice or the error function
Wrap your choice and num input in a while loop, break when the user chooses "exit":
def correct_result(choice,num):
if choice.lower() == 'square': #Prints the Square of a number Ex. 2^2 = 4
return num**2
elif choice.lower() == 'sqrt': #Prints the Square root of a number Ex. √4 = 2
return num**.5
elif choice.lower() == 'reverse': #Changes the sign of the number
return(-num)
else:
return 'Invalid Choice' #prints an error message
while True:
choice = input("Choice: ") #Creates a answer box to enter the desired choice
if choice == "exit":
exit()
num = int(input("Number: ")) #Creates a second box that lets the user enter their number
print(correct_result(choice,num)) #Prints either the desired choice or the error function

Categories

Resources