[英]How do I fix this 'NoneType' error in my code?
我刚刚开始学习python并编写了一个包含三个函数的脚本。 在 Q 输入通过后,我收到一个 Traceback 错误。
这只是一个学习练习,所以我可以发展我的 Python 技能。 学习模块源代码和我的代码看起来完全一样,但由于某种原因我的输出返回了一个错误。
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'
您的get_order
函数没有返回任何内容,因此该行中的orders
值:
orders = get_order(menu)
将None
。 然后你试图迭代变量,它会失败。
您需要在get_order
函数的末尾添加这一行:
return orders
所以函数应该是这样的:
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
原因是orders
是None
,这是因为函数get_order
返回任何内容。
看看这一行:
orders = get_order(menu)
你应该添加:
return orders
在get_order
函数结束时。 像这样:
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
您的get_order()
函数没有return
语句。 因此,它将始终返回None
。 因此,在main()
:
orders = get_order(menu)
total = bill_total(orders, menu)
orders
将获得None
值,然后将其传递给bill_total()
。 当您尝试在 for 循环中迭代它时,结果是您看到的异常
迟到的答案,但您可以使用:
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()}" )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.