I'm just starting to learn python and have written a script with three functions. After the Q input is passed through I receive a Traceback error.
This is just a learning exercise so I can develop my Python skills. The learning module source code and my code appear to be exactly the same, but for some reason my output returns an error.
File "C:\Users\Desktop\DearWorld\new.py", line 36, in <module>
main()
File "C:\Users\Desktop\DearWorld\new.py", line 33, in main
total = bill_total(orders, menu)
File "C:\Users\Desktop\DearWorld\new.py", line 24, in bill_total
for order in orders:
TypeError: 'NoneType' object is not iterable
menu = {'Angel Dust Blunt': 6.66, 'The OG Blunt': 4.20, 'Caviar Blunt': 7.10, 'The Chronic Blunt' : 4.20}
def print_menu(menu):
for name, price in menu.items():
print(name, ': $', format(price, '.2f'), sep = '')
def get_order(menu):
orders = []
order = input("What would you like to order (Q to quit)")
while (order.upper() != 'Q'):
#Find the order and add it to the list if it exists
found = menu.get(order)
if found:
orders.append(order)
else:
print("Menu item doesn't exist")
order = input("Anything else? (Q to Quit)")
def bill_total(orders, menu):
total = 0
for order in orders:
total += menu[order]
return total
def main():
menu = {'Angel Dust Blunt': 6.66, 'The OG Blunt': 4.20, 'Caviar Blunt': 7.10, 'The Chronic Blunt' : 4.20}
print_menu(menu)
orders = get_order(menu)
total = bill_total(orders, menu)
print("You ordered:" ,order, "Your total is: $", format(total, '.2f'), sep='')
main()
The script is supposed to return the bill_total and the items ordered as the output. What is returned instead when the user enters 'Q' is a 'TypeError'
Your get_order
function isn't returning anything, so the value of orders
in the line:
orders = get_order(menu)
will be None
. Then you're trying to iterate over the variable, and it will fail.
You need to add this line at the end of the get_order
function:
return orders
So the function should look like this:
def get_order(menu):
orders = []
order = input("What would you like to order (Q to quit)")
while (order.upper() != 'Q'):
#Find the order and add it to the list if it exists
found = menu.get(order)
if found:
orders.append(order)
else:
print("Menu item doesn't exist")
order = input("Anything else? (Q to Quit)")
return orders # Added this line
The reason is that orders
is None
, and it is because the function get_order
returns nothing.
look at this line:
orders = get_order(menu)
you should add:
return orders
at the end of get_order
function. like this:
def get_order(menu):
orders = []
order = input("What would you like to order (Q to quit)")
while (order.upper() != 'Q'):
#Find the order and add it to the list if it exists
found = menu.get(order)
if found:
orders.append(order)
else:
print("Menu item doesn't exist")
order = input("Anything else? (Q to Quit)")
return orders
Your get_order()
function has no return
statement. As a result, it will always return None
. Therefore, in main()
:
orders = get_order(menu)
total = bill_total(orders, menu)
orders
will get a value of None
, which is then passed to bill_total()
. When you try to iterate over that in the for loop, the result is the exception you're seeing
Late answer, but you can use:
menu = {
"1": {"name":"Angel Dust Blunt", "price":6.66},
"2": {"name":"The OG Blunt", "price":6.66},
"3": {"name":"Caviar Blunt", "price":7.10},
"4": {"name":"The Chronic Blunt", "price":4.20}
}
orders = []
def print_menu():
for k, v in menu.items():
print(f"[{k}] - ", v['name'], ': $', format(v['price'], '.2f'), sep = '')
def bill_total():
total = 0
for id in orders:
total += menu[id]['price']
return total
def get_order():
while 1:
order = input(f"What would you like to order (Q to quit)\n")
if order.strip().upper() == 'Q':
return
else:
found = menu.get(order)
if found:
# avoid dup orders, can be improved to increase the count of product items.
if order in orders:
print("Product already in cart")
continue
orders.append(order)
print(f"Item {found['name']} added to cart")
else:
print("Menu item doesn't exist")
print_menu(), get_order(), print("You ordered:\n")
for id in orders:
print(menu[id]['name'], f"${menu[id]['price']}")
print(f"\nYour total is: ${bill_total()}" )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.