Python 当用户提供某个关键字作为输入时,结束函数

Python 当用户提供某个关键字作为输入时,结束函数,python,input,output,Python,Input,Output,当用户在输入中键入特定关键字(例如“off”)而不必在每个输入中添加必要的代码时,如何结束函数?基本上,当用户在下面的任何输入中键入“off”时,我希望函数完全停止。我已经创建了一个函数(见代码底部),并尝试在我的代码中放置/调用它,但不确定确切地将其放置在何处,或者是否可以使用函数执行此操作?如果没有,我如何才能做到这一点 def order_coffee(): drink = input("What would you like? (espresso/latte/ca

当用户在输入中键入特定关键字(例如“off”)而不必在每个输入中添加必要的代码时,如何结束函数?基本上,当用户在下面的任何输入中键入“off”时,我希望函数完全停止。我已经创建了一个函数(见代码底部),并尝试在我的代码中放置/调用它,但不确定确切地将其放置在何处,或者是否可以使用函数执行此操作?如果没有,我如何才能做到这一点

  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

创建自己的
输入
功能并添加所需的行为:

def my_input(*args, **kwargs):
    str = input(*args, **kwargs)
    if str == "off":
        exit()
    return str
当用户输入为“off”时,这将从整个应用程序中退出,只从函数中退出,而不是抛出一个异常,并在函数调用之外捕获它。例如:

def my_input(*args, **kwargs):
    str = input(*args, **kwargs)
    if str == "off":
        raise ValueError('Time to go')
以及:


更清晰的方法是创建、抛出和处理自定义的
异常。查看此项,了解如何实现这些自定义异常。

您可以创建自己的输入函数来检查此项,当输入“关闭”事件时,退出程序。这样,如果有人在程序中的任何一点输入
“off”
,程序将结束

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()
导入系统 def get_输入(文本): 用户反馈=输入(文本) 如果用户反馈==“关闭”: sys.exit() 返回用户反馈 def order_coffee(): 打印(“欢迎使用咖啡机器人!”(键入“关闭”随时退出) 饮料=获取输入(“您想要什么?(浓缩咖啡/拿铁/卡布奇诺):”) 用户_owes=int(0) 如果饮料==“浓缩咖啡”: 用户欠=整数(1.5,2) elif饮料==“拿铁”: 用户欠=整数(2.5,2) elif饮料==“卡布奇诺”: 用户欠=整数(3.0,2) 打印(f“请插入硬币,您欠${user_owes}。”) 季度=(整数(获取单位输入(“多少季度:”)))*.25) dimes=(int(获取输入(“多少个dimes:”)))*1) 镍币=(整数(获取输入(“多少镍币:”))*.05) 便士=(int(获取输入(“多少便士:”))).01) 用户付费=圆形(四分之一硬币+一角硬币+五分镍币+一分硬币,2) 打印(f“您已插入${user\u paid}。”) 更改=轮次(用户付费-用户欠费,2) 如果用户付费>=用户欠款: 打印(f“此处为${change}更改中。”) 打印(f)“这是你的{饮料}”☕. 享受!) 点咖啡 其他: 打印(“对不起,钱不够。退款。”) 点咖啡 点咖啡
请更正您的代码。函数
get\u input
不返回输出。捕捉得不错,修复了它!
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()