当用户提供某个关键字作为输入时结束函数
End a function when the user provides a certain keyword as input
当用户在输入中键入特定关键字(例如 'off')时,我如何结束函数而不必在每个输入中添加必要的代码?基本上我希望当用户在下面的任何输入中的任何地方键入单词 'off' 时该功能完全停止。我已经创建了一个函数(见代码底部)并在我的代码中尝试了 placing/calling 但我不确定将它放在哪里或者我是否可以用一个函数来做到这一点?如果没有,我该如何实现?
def order_coffee():
drink = input("What would you like? (espresso/latte/cappuccino): ")
user_owes = int(0)
if drink == "espresso":
user_owes = round(1.5, 2)
elif drink == "latte":
user_owes = round(2.5, 2)
elif drink == "cappuccino":
user_owes = round(3.0, 2)
print(f"Please insert coins, you owe ${user_owes}.")
quarters = (int(input("How many quarters?: ")) * .25)
dimes = (int(input("How many dimes?: ")) * .1)
nickels = (int(input("How many nickels?: ")) * .05)
pennies = (int(input("How many pennies?: ")) * .01)
user_paid = round(quarters + dimes + nickels + pennies, 2)
print(f"You have inserted ${user_paid}.")
change = round(user_paid - user_owes, 2)
if user_paid >= user_owes:
print(f"Here is ${change} in change.")
print(f"Here is your {drink} ☕. Enjoy!")
order_coffee()
else:
print("Sorry that's not enough money. Money refunded.")
order_coffee()
order_coffee()
def off():
if input == "off":
return
创建您自己的 input
函数并添加所需的行为:
def my_input(*args, **kwargs):
str = input(*args, **kwargs)
if str == "off":
exit()
return str
当用户输入为“关闭”时,这将从整个应用程序退出,仅从函数退出而不是抛出异常,并在函数调用之外捕获它。例如:
def my_input(*args, **kwargs):
str = input(*args, **kwargs)
if str == "off":
raise ValueError('Time to go')
和:
def order_coffee():
try:
# your code
except ValueError as err:
# handle it accordingly
return
更清晰的方法是创建、抛出和处理自定义 Exception
。看看这个 SO Thread 以了解如何实现这些自定义异常。
def order_coffee():
drink = input("What would you like? (espresso/latte/cappuccino): ")
user_owes = int(0)
if drink == "espresso":
user_owes = round(1.5, 2)
elif drink == "latte":
user_owes = round(2.5, 2)
elif drink == "cappuccino":
user_owes = round(3.0, 2)
elif drink == 'off':
return None
print(f"Please insert coins, you owe ${user_owes}.")
quarters = (int(input("How many quarters?: ")) * .25)
dimes = (int(input("How many dimes?: ")) * .1)
nickels = (int(input("How many nickels?: ")) * .05)
pennies = (int(input("How many pennies?: ")) * .01)
user_paid = round(quarters + dimes + nickels + pennies, 2)
print(f"You have inserted ${user_paid}.")
change = round(user_paid - user_owes, 2)
if user_paid >= user_owes:
print(f"Here is ${change} in change.")
print(f"Here is your {drink} ☕. Enjoy!")
order_coffee()
else:
print("Sorry that's not enough money. Money refunded.")
order_coffee()
order_coffee()
您可以创建自己的输入函数来检查这一点,并在输入事件 'off' 时退出程序。这样,如果有人在程序中的任何时候输入 "off"
,它就会结束。
import sys
def get_input(text):
user_feedback = input(text)
if user_feedback == 'off':
sys.exit()
return user_feedback
def order_coffee():
print("Welcome to the coffee robot! (type 'off' to exit at any time)")
drink = get_input("What would you like? (espresso/latte/cappuccino): ")
user_owes = int(0)
if drink == "espresso":
user_owes = round(1.5, 2)
elif drink == "latte":
user_owes = round(2.5, 2)
elif drink == "cappuccino":
user_owes = round(3.0, 2)
print(f"Please insert coins, you owe ${user_owes}.")
quarters = (int(get_input("How many quarters?: ")) * .25)
dimes = (int(get_input("How many dimes?: ")) * .1)
nickels = (int(get_input("How many nickels?: ")) * .05)
pennies = (int(get_input("How many pennies?: ")) * .01)
user_paid = round(quarters + dimes + nickels + pennies, 2)
print(f"You have inserted ${user_paid}.")
change = round(user_paid - user_owes, 2)
if user_paid >= user_owes:
print(f"Here is ${change} in change.")
print(f"Here is your {drink} ☕. Enjoy!")
order_coffee()
else:
print("Sorry that's not enough money. Money refunded.")
order_coffee()
order_coffee()
当用户在输入中键入特定关键字(例如 'off')时,我如何结束函数而不必在每个输入中添加必要的代码?基本上我希望当用户在下面的任何输入中的任何地方键入单词 'off' 时该功能完全停止。我已经创建了一个函数(见代码底部)并在我的代码中尝试了 placing/calling 但我不确定将它放在哪里或者我是否可以用一个函数来做到这一点?如果没有,我该如何实现?
def order_coffee():
drink = input("What would you like? (espresso/latte/cappuccino): ")
user_owes = int(0)
if drink == "espresso":
user_owes = round(1.5, 2)
elif drink == "latte":
user_owes = round(2.5, 2)
elif drink == "cappuccino":
user_owes = round(3.0, 2)
print(f"Please insert coins, you owe ${user_owes}.")
quarters = (int(input("How many quarters?: ")) * .25)
dimes = (int(input("How many dimes?: ")) * .1)
nickels = (int(input("How many nickels?: ")) * .05)
pennies = (int(input("How many pennies?: ")) * .01)
user_paid = round(quarters + dimes + nickels + pennies, 2)
print(f"You have inserted ${user_paid}.")
change = round(user_paid - user_owes, 2)
if user_paid >= user_owes:
print(f"Here is ${change} in change.")
print(f"Here is your {drink} ☕. Enjoy!")
order_coffee()
else:
print("Sorry that's not enough money. Money refunded.")
order_coffee()
order_coffee()
def off():
if input == "off":
return
创建您自己的 input
函数并添加所需的行为:
def my_input(*args, **kwargs):
str = input(*args, **kwargs)
if str == "off":
exit()
return str
当用户输入为“关闭”时,这将从整个应用程序退出,仅从函数退出而不是抛出异常,并在函数调用之外捕获它。例如:
def my_input(*args, **kwargs):
str = input(*args, **kwargs)
if str == "off":
raise ValueError('Time to go')
和:
def order_coffee():
try:
# your code
except ValueError as err:
# handle it accordingly
return
更清晰的方法是创建、抛出和处理自定义 Exception
。看看这个 SO Thread 以了解如何实现这些自定义异常。
def order_coffee():
drink = input("What would you like? (espresso/latte/cappuccino): ")
user_owes = int(0)
if drink == "espresso":
user_owes = round(1.5, 2)
elif drink == "latte":
user_owes = round(2.5, 2)
elif drink == "cappuccino":
user_owes = round(3.0, 2)
elif drink == 'off':
return None
print(f"Please insert coins, you owe ${user_owes}.")
quarters = (int(input("How many quarters?: ")) * .25)
dimes = (int(input("How many dimes?: ")) * .1)
nickels = (int(input("How many nickels?: ")) * .05)
pennies = (int(input("How many pennies?: ")) * .01)
user_paid = round(quarters + dimes + nickels + pennies, 2)
print(f"You have inserted ${user_paid}.")
change = round(user_paid - user_owes, 2)
if user_paid >= user_owes:
print(f"Here is ${change} in change.")
print(f"Here is your {drink} ☕. Enjoy!")
order_coffee()
else:
print("Sorry that's not enough money. Money refunded.")
order_coffee()
order_coffee()
您可以创建自己的输入函数来检查这一点,并在输入事件 'off' 时退出程序。这样,如果有人在程序中的任何时候输入 "off"
,它就会结束。
import sys
def get_input(text):
user_feedback = input(text)
if user_feedback == 'off':
sys.exit()
return user_feedback
def order_coffee():
print("Welcome to the coffee robot! (type 'off' to exit at any time)")
drink = get_input("What would you like? (espresso/latte/cappuccino): ")
user_owes = int(0)
if drink == "espresso":
user_owes = round(1.5, 2)
elif drink == "latte":
user_owes = round(2.5, 2)
elif drink == "cappuccino":
user_owes = round(3.0, 2)
print(f"Please insert coins, you owe ${user_owes}.")
quarters = (int(get_input("How many quarters?: ")) * .25)
dimes = (int(get_input("How many dimes?: ")) * .1)
nickels = (int(get_input("How many nickels?: ")) * .05)
pennies = (int(get_input("How many pennies?: ")) * .01)
user_paid = round(quarters + dimes + nickels + pennies, 2)
print(f"You have inserted ${user_paid}.")
change = round(user_paid - user_owes, 2)
if user_paid >= user_owes:
print(f"Here is ${change} in change.")
print(f"Here is your {drink} ☕. Enjoy!")
order_coffee()
else:
print("Sorry that's not enough money. Money refunded.")
order_coffee()
order_coffee()