python - 如何阻止此程序弹出?
问题描述
我正在用 python 做这家咖啡店,我想让顾客重新选择尺寸,但是一旦选择了尺寸,它又回到了饮料类型,get_drink_type 会出现两次。我怎样才能让它只出现一次?我之前已经尝试过放置一个 for 循环,但它实际上并没有解决问题。
def print_message():
print('''I'm sorry, I did not understand your selection. Please enter the corresponding letter for your response.''')
def get_size():
res = input('What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ')
redo = get_drink_type()
if res == 'a':
if redo == 'd':
get_drink_type()
return 'small'
elif res == 'b':
if redo == 'd':
get_drink_type()
return 'medium'
elif res == 'c':
if redo == 'd':
get_drink_type()
return 'large'
else:
print_message()
return get_size()
def get_drink_type():
kind = input('What type of drink would you like? \n[a] Brewed Coffee \n[b] Mocha \n[c] Latte \n[d] Return to the size \n> ')
if kind == 'a':
return 'brewed coffee'
elif kind == 'b':
return 'mocha'
elif kind == 'c':
return order_latte()
elif kind == 'd':
return get_size()
else:
print_message()
return get_drink_type()
def order_latte():
latte = input('And what kind of milk for your latte? \n[a] 2% milk \n[b] Non-fat milk \n[c] Soy milk \n> ')
if latte == 'a':
return 'latte'
elif latte == 'b':
return 'non-fat latte'
elif latte == 'c':
return 'soy latte'
else:
print_message()
return order_latte()
def get_desert():
desert = input('Would you like an extra dessert with your coffee? \n[a] 5 cookies \n[b] An icecream \n[c] A brownie \n[d] No, thank you \n>')
if desert == 'a':
return 'cookies'
elif desert == 'b':
return get_icecream_flavour()
elif desert == 'c':
return get_brownie()
elif desert == 'd':
return 'nothing'
else:
print_message()
return get_desert()
def get_brownie():
brownie = input('Do you want ice cream with your brownie? \n[a] Yes \n[b] No \n>')
if brownie =='a':
return get_icecream_brownie()
elif brownie == 'b':
return 'brownie with no ice cream'
else:
print_message()
return get_brownie()
def get_icecream_brownie():
flavour = input('What flavour would you like your ice cream to be? \n[a] Vanilla \n[b] Chocolate \n[c] Strawberry \n>')
if flavour == 'a':
return 'brownie with a vanilla ice cream'
elif flavour == 'b':
return 'brownie with a chocolate ice cream'
elif flavour == 'c':
return 'brownie with a strawberry ice cream'
else:
print_message()
return get_icecream_brownie()
def get_icecream_flavour():
flavour = input('What flavour would you like your ice cream to be? \n[a] Vanilla \n[b] Chocolate \n[c] Strawberry \n>')
if flavour == 'a':
return 'vanilla ice cream'
elif flavour == 'b':
return 'chocolate ice cream'
elif flavour == 'c':
return 'strawberry ice cream'
else:
print_message()
return get_icecream_flavour()
# Define your functions
def coffee_bot():
print('''Welcome to the cafe!''')
name = input('Can I get you name pls? \n> ')
size = get_size()
if size == 'small':
a = 1.00
elif size == 'medium':
a = 1.20
else:
a = 1.50
drink_type = get_drink_type()
if drink_type == 'brewed coffee':
b = 1.50
elif drink_type == 'mocha':
b = 1.20
elif drink_type == 'latte':
b = 1.40
elif drink_type == 'non-fat latte':
b = 1.80
elif drink_type == 'soy latte':
b = 2.00
dessert = get_desert()
if dessert == 'cookies':
c = 1.20
elif dessert == 'vanilla ice cream':
c = 1.40
elif dessert == 'chocolate ice cream':
c = 1.40
elif dessert == 'strawberry ice cream':
c = 1.40
elif dessert == 'nothing':
c = 0.0
elif dessert == 'brownie with no ice cream':
c = 4.00
else:
c = 5.40
d = format(a + b + c, '.2f')
d = str(d)
if dessert == 'nothing':
first_message = '''Alright, that's a ''' + size +' '+ drink_type + ' wtih no dessert and a total price of ' + d + '€'
print(first_message)
print ('''Thanks, ''' +name+'''! Your order will be ready shortly.''')
elif dessert == 'cookies':
first_message = '''Alright, that's a ''' + size +' '+ drink_type + ' plus 5 ' + dessert + ' with a total price of ' + d + '€'
print(first_message)
print ('''Thanks, ''' +name+'''! Your order will be ready shortly.''')
else:
first_message = '''Alright, that's a ''' + size +' '+ drink_type + ' plus a ' + dessert + ' with a total price of ' + d + '€'
print(first_message)
print ('''Thanks, ''' +name+'''! Your order will be ready shortly.''')
coffee_bot()
解决方案
推荐阅读
- python - 从列表的每个元素中剥离 \n
- c - 为什么 The C Programming Language 书中的一些代码示例不能按预期工作?
- android - 颤动应用程序更改状态栏以匹配相同的应用程序栏
- html - 防止网格上的双边框
- amazon-s3 - 如何使用 Pyspark(在 macos 中)从 s3 存储桶中读取 csv 文件?
- udp - Windows 防火墙问题 - 无法使用 GameRanger
- python - 在不更改索引排序的情况下对分组的 DataFrame 列进行排序
- javascript - 语法错误:'import' 和 'export' 可能只出现在顶层 - 尽管在 ESLint 中设置了规则
- python - pandas:将 int Series 转换为新的 StringDtype
- mysql - 将两张表合并为一张表,无需使用连接,也无需创建笛卡尔积