beginner here, im currently creating a coffee shop program in pycharm where the costumer inputs the product and then the program sums up all of the price. Im trying to loop the program with the use of functions so the customer can order a many as they want. But when i inputted def main(): in the program, the program under the function does not continue when i run it and i made sure that the codes under the function are indented. Any help would be appreciated thanks enter image description here
here's the code, sorry
print("Good day!, welcome to Avenida's coffee shop")
print("Before i take you order, may i know your name?")
name = input("Input name here: ")
print(
"Hello there " + name + "!, to get started we sell different kinds of food here, we sell coffee, frappe, cake, and pastas")
print("To access our menu, input your preferred food below")
def main():
total_price = 0
category = input("Input your preferred food here: ")
if category.lower() == "coffee":
print("Coffee Menu:")
print("1 Cappuccino = $2")
print("2 Espresso = $2.50")
print("3 Latte = $3")
coffee_input = input("Input the assigned number of the coffee that you would like to order: ")
if int(coffee_input) == 1:
print("How many cappuccino would you like to order?")
coffee_quantity = input("Input here: ")
coffee_total = float(coffee_quantity) * 2
print("That would be $" + str(coffee_total))
total_price = total_price + coffee_total
if int(coffee_input) == 2:
print("How many espresso would you like to order?")
coffee_quantity = input("Input here: ")
coffee_total = float(coffee_quantity) * 2.50
print("That would be $" + str(coffee_total))
total_price = total_price + coffee_total
if int(coffee_input) == 3:
print("How many latte would you like to order?")
coffee_quantity = input("Input here: ")
coffee_total = float(coffee_quantity) * 3
print("That would be $" + str(coffee_total))
total_price = total_price + coffee_total
Try adding this to the end of your code, outside of your main loop. You defined a function called main, but you did not execute it, thus the issue.
if __name__ == '__main__':
main()
Related
I'm a beginner in python & this is my 3rd program.
I'm learning functions. The user should be able to choose a number first from the menu (1. appetizers, 2.mains 3. drinks, 4.view orders, 5.exit) each one has sub-choices (example appetizers include salad, chips...etc). User should be able to return back after choosing an item.
I'm stuck in View Order option, the user needs to be able to see each of the items in their order + prices next to each item & Lastly I want to be able to show total bill.
How can i view all orders from user?
How can i calculate total? should i use loop?
def menu():
print("--MCQUAN MENU--")
print("1. Appetizers")
print("2. Mains")
print("3. Drinks")
print("4. Desserts")
print("5. View order")
print("6. Exit")
print()
def user_option():
choice = int(input("Choose from the menu & enter a number 1-6: "))
if choice == 1:
print()
print("APP - heres the apps")
print("Salad - $5")
print("Chips & salsa - $6")
print("Soup - $10")
elif choice == 2:
print("MAINS - here the mains")
print("Pasta - $13")
print("Burger - $12")
print("Pizza - $10")
elif choice == 3:
print("DRINKS - heres drinks")
print("Water - $0")
print("Tea - $3")
print("Sprite - $2")
elif choice == 4:
print("DESSERTS - heres desserts")
print("Cake - $6")
print("Ice cream - $5 ")
print("Pie - $7")
elif choice == 5:
print("VIEW ORDER -- heres ur order so far")
elif choice == 6:
print("Exiting")
else:
print("Invalid")
choice = int(input("Enter number 1-6"))
def app():
cost = 0
total = 0
user_food = input("Choose which u want ")
if user_food == "salad":
cheese = input("Cheese? (+$0.50) yes/no ")
dressing = input("Dressing? (+$1) yes/no ")
elif user_food == "chips & salsa":
spicy = input("Spicy? yes/no ")
cilantro = input("Cilantro? yes/no ")
elif user_food == "soup":
soup = input("Chicken noodle or mushroom? ")
else:
print("Alright!")
user_option()
user_food = input("Choose which u want ")
def main():
user_food = input("Choose which u want ")
if user_food == "pasta":
cheese_2 = input("Cheese? yes/no ")
chilli_f = input("Chilli flakes? yes/no ")
elif user_food == "burger":
onions = input("Onions? yes/no ")
pickles = input("Pickles? yes/no ")
elif user_food == "Pizza":
pizza = input("Cheese or pepperoni? ")
else:
print("Alright!")
user_option()
user_food = input("Choose which u want ")
def drinks():
user_food = input("Choose which u want ")
if user_food == "water":
size = input("Small, medium or large? ")
user_option()
user_food = input("Choose which u want ")
def desserts():
user_food = input("Choose which you want")
if user_food == "cake":
w_c = input("Whipped cream? yes/no ")
cherry = input("Cherry? yes/no ")
elif user_food == "ice cream":
w_c = input("Whipped cream? yes/no")
cherry = input("Cherry? yes/no ")
elif user_food == "pie":
w_c = input("Whipped cream? yes/no")
cherry = input("Cherry? yes/no ")
else:
print("Alright")
user_option()
user_food = input("Choose which u want ")
#main
menu()
print("Welcome to McQuans!")
print()
user_option()
app()
main()
drinks()
desserts()`
Ideally you do not want to keep your data in prints.
We can use data structures like lists or dicts
Here's what I suggest:
We use a list for the client order, since we can keep adding to it and we'll be adding things sequentially.
We use dicts (python dictionaries) for the menus, so we can keep track of all the items per menu and the price of each item.
client_order = []
app_menu = {
"Salad": 5,
"Chips & salsa": 6,
"Soup": 10
}
Please check out this Dict tutorial if you are not familiar with them.
Alright, let's start with this. Once you've got it I'll help you out some more if you need.
My program has errors and bugs that I need to fix. It's about a "robot" that is a waiter in a restaurant. I am still a beginner.
I tried looking through my program for the bugs and tried indenting and dedenting different parts of my program to try and get it working. I als o tried messing around with the operators but nothing seems to work.
import sys, time
total = 0
menu = ["0 - Swedish Meatballs - 5$", "1 - Japanese Sushi - 7$", "2 - Indian Rice - 9$", "3 - Quit"]
price = [5,7,9,0]
print("Hello welcome to the restaurant! I am your robotic waiter")
action = int(input("What would you like to do? \n 1. Look at the menu \n 2. Order \n 3. Take more time \n 4. Ask for the total \n 5. Exit \n Please enter a number here: "))
while action != 5:
if action == 1:
print(menu)
action = int(input("What would you like to do? \n 1. Look at the menu \n 2. Order \n 3. Take more time \n 4. Ask for the total \n 5. Exit \n Please enter a number here: "))
elif action == 2:
print(menu)
food = int(input("What would you like? "))
while food != 3:
priceoffood = price[food]
total = total + priceoffood
if food != 3:
more = input("More things? Reply with y or n: ")
if more == "y":
print(menu)
food = int(input("What would you like? "))
if priceoffood != 3:
print(food)
print(price[food])
priceoffood = price[food]
total = total + priceoffood
else:
break
elif action == 3:
time = int(input("How many minutes more do you need? "))
while int(time) > 30:
print ("Isn't that too long? ")
time = input("How many minutes more do you need? ")
print("Okay, ill be back when your " + str(time) + " minutes are over!")
time.sleep(time*60)
elif action == 4:
print("Your total is: " + str(total))
quit()
I would like the menu functions to be working just like how they are expected to be.
You imported time, but also used it as a variable. Change your variable to something other than time. For example:
timer = int(input("How many minutes more do you need? "))
while int(timer) > 30:
print ("Isn't that too long? ")
timer = input("How many minutes more do you need? ")
print("Okay, ill be back when your " + str(timer) + " minutes are over!")
time.sleep(timer*60)
Transaction_Code == "W", "w", "D" or "d"
if not, it should be running Process_Invalid_Code(Previous_Balance)
What is happening, however is if input for Transaction_Code != "W", "w", "D" or "d", it then continues to run the "What is your previous balance?" and "How much is the transaction amount?" input...
ONLY THEN, after you give input for those does it run Invalid_Transaction_Code
What I WANT to happen is for it to throw Invalid_Transaction_Code ("Invalid Transaction Code!") etc. BEFORE without wasting the users time asking for Previous Balance and Transaction..
Does that make sense?
Here is the code
#The main function definition
def main():
Name = input("What is your name? ")
Account_ID = input("What is your account ID? ")
Transaction_Code = input("Press W or w for Withdrawal, Press D or d for Deposit: ")
Previous_Balance = float(input("What is your previous balance? "))
Transaction_Amount = float(input("How much is the transaction amount? "))
if Transaction_Code == "W" or Transaction_Code == "w":
Withdrawal_Process(Previous_Balance, Transaction_Amount)
elif Transaction_Code == "D" or Transaction_Code == "d":
Deposit_Process(Previous_Balance, Transaction_Amount)
else:
Process_Invalid_Code(Previous_Balance)
#Defines the Deposit Process
def Deposit_Process(Previous_Balance, Transaction_Amount):
New_Balance = Transaction_Amount + Previous_Balance
Print_Function(New_Balance)
#Defines the Withdrawal Process
def Withdrawal_Process(Previous_Balance, Transaction_Amount):
if Transaction_Amount > Previous_Balance:
print("Invalid Transaction: Not Sufficient Funds!")
New_Balance = Previous_Balance
Print_Function(New_Balance)
else:
New_Balance = Previous_Balance - Transaction_Amount
Print_Function(New_Balance)
#The Invalid Code Function Definition
def Process_Invalid_Code(Previous_Balance):
New_Balance = Previous_Balance
print ("Invalid Transaction Code!")
print ("Please type W or w for Withdrawal")
print ("or type D or d for Deposit")
Print_Function(New_Balance)
#Defines the Print Function
def Print_Function(New_Balance):
print ("Your balance is now $", format(New_Balance, '.2f'))
#Call the main function
main()
Since all of your desired actions need Previous_Balance you must ask for it in any case:
def main():
# never used, lets ask anyway
Name = input("What is your name? ")
# we need this information at a minimum
Previous_Balance = float(input("What is your previous balance? "))
Transaction_Code = input("Press W or w for Withdrawal, Press D or d for Deposit: ")
# if its a withdrawal/deposit, find the amount and account
if Transaction_Code.upper() in "WD":
# we never use this Account_ID ...
Account_ID = input("What is your account ID? ")
Transaction_Amount = float(input("How much is the transaction amount? "))
if Transaction_Code.upper() == "W":
Withdrawal_Process(Previous_Balance, Transaction_Amount)
else:
Deposit_Process(Previous_Balance, Transaction_Amount)
else:
# they've entered a bad code
Process_Invalid_Code(Previous_Balance)
You may want to also try and use raw_input() as oppose to input() where necessary or cast the output using variable_name to get rid of tracback error.
so I've been working on this code for a little bit just to test my abilities and I've run into a brick wall. For some reason my code is breaking out of my for loop. If I say I want to see how much 2 rooms cost it goes through the first one fine but them asks me how many rooms I want to check again. If I input 2 again, it will go through it once and then go on through the rest of the code normally. I have two for loops setup the exact same as each other but for some reason one works and the other doesn't. I'm probably making a stupid mistake but I've been trying to fix it for ages now but no cigar. Appreciate all help!
def start():
print ("Welcome to Caroline's Painting and Decorating cost estimator")
print ("Here you can see an estimate to see how much the job will cost")
print ("First though, you have to enter a few details about the job")
customerNumber = input ("Please enter your customer number ")
dateofEstimate = input ("Please enter the current date ")
def roomCalculator():
surfaceAreaCost = 0
totalPrice = 0
price = 0
count = 0
rooms = int(input("Please enter the number of rooms that require painting "))
for i in range(0, rooms):
roomName = input ("What room needs decorating? ")
wallsNumber = int(input("How many walls are in the room? "))
wallpaper = input ("Is there any wallpaper thats needs to be removed? Enter Y for yes and N for no ")
if wallpaper == "Y".lower():
surfaceAreaCost + 70
for i in range(0, wallsNumber):
count = count + 1
print ("Please enter the dimensions for wall", count)
height = int(input("How high is the wall? "))
width = int(input("How long is the wall? "))
surfaceAreaWall = height*width
wallCost = surfaceAreaWall * 15
price = surfaceAreaCost + wallCost
employeeType = input ("What employee would you like? Enter AP for apprentice or FQ for Fully-Qualified ")
if employeeType == "AP".lower():
price = price + 100
print ("You are having an apprentice do the job")
print ("The price for the", roomName, "without V.A.T is", price)
return price
elif employeeType == "FQ".lower():
price = price + 250
print ("You are having a Fully-Qualified employee to do the job")
print ("The price for the", roomName, "without V.A.T is", price)
return price
Your error is a result of nested for loops, because you use variable i in both. This causes an error since you change value of variable i in first for loop with second for loop. Rename variable in second for loop with j for instance.
Thanks all for the help! It was a stupid error on my part. I used 3.4.4 at my school computer and 3.5.1 at home. It all worked anyway.
So I'm coding a small project and I'm struggling with a certain aspect so far.
Here is the code:
import re
def clientDetails():
print("Welcome to the InHouse Solutions Room Painting Price Calculator")
print("STEP 1 - CLIENT DETAILS")
print("Please enter your full name")
userName = input(">>>")
print("Please enter your post code")
postCode = input(">>>")
print("Is your house a number, or a name?")
nameOrNumber = input(">>>")
if nameOrNumber == "number" or nameOrNumber == "Number":
print("Please enter your house number")
houseNumber = input(">>>")
elif nameOrNumber == "Name" or nameOrNumber == "name":
print("Please enter your house name")
houseName = input(">>>")
else:
print("Invalid")
house = (houseNumber) + (houseName)
address = (postCode) + ", " + (house)
print("Thank you for your information")
print (userName)
print (address)
print (" ")
print ("Is this information correct? Pleast enter Yes or No")
clientDetailsCorrect = input(">>>")
if clientDetailsCorrect == "no" or clientDetailsCorrect == "No":
clientDetails()
clientDetails()
Not sure what's going wrong as I haven't actually referenced the variable anywhere else. Someone help.
It would help if you posted the traceback.
That said, this line is the likely source of the problem:
house = (houseNumber) + (houseName)
The way your code is currently written, only one of houseNumber or houseName will be defined. So Python is likely complaining about the missing one.
Given how your code looks so far, it's probably better to just do:
print("Please enter your house name or number")
house = input(">>>")
And remove the house = (houseNumber) + (houseName) line.
Try this:
def clientDetails():
print("Welcome to the InHouse Solutions Room Painting Price Calculator\n")
print("STEP 1 - CLIENT DETAILS")
print("Please enter your full name")
userName = raw_input(">>>")
print("Please enter your post code")
postCode = raw_input(">>>")
print("Please enter your hose number or name")
house = raw_input(">>>")
address = "{}, {}".format(postCode, house)
print("Thank you for your information.\n")
print (userName)
print (address)
print (" ")
print ("Is this information correct? Pleast enter Yes or No")
clientDetailsCorrect = raw_input(">>>")
if clientDetailsCorrect.lower().startswith('n'):
clientDetails()
Using raw_input is better, it will input everything as a string. Also it will allow users to not type quotations to input text (I assume you will run this from CLI). If you later on need to separate houses that are numbers from names Python has very good string methods you can use to do so many wonderful things, I used a couple of them to simplify your code :)
N