【发布时间】:2024-01-01 05:08:01
【问题描述】:
考虑下面的代码:
MENU = {'sandwich': 10, 'tea': 7, 'pizza': 10, 'soda':3, 'burger': 10}
def restaurant():
total = 0
while True:
order = input('Order: ').lower().strip()
if not order:
break
if order in MENU:
price = MENU[order]
total += price
**return f'{order} is {price}, total is {total}' #will not loop
print(f'{order} is {price}, total is {total}') #will loop**
else:
print(f'Sorry, we are fresh out of {order} today')
print(f'Your total is {total}')
restaurant()
问题是:
为什么while循环不循环通过return,而是循环通过print?我一直在学习python,但现在才指出这个场合。
【问题讨论】:
-
"return" 结束函数并返回一个值,"print" 只是将值打印到控制台上
-
print和return没有任何共同之处(尽管 REPL 有打印表达式值的做法)。 -
你觉得
return应该怎么做?
标签: python loops while-loop printing return