2

How can I end a function when a user types a specific keyword (e.g. 'off') in an input without having to add the necessary code within each input? Basically I want the function to completely stop when a user types the word 'off' anywhere in any of the inputs below. I have created a function (see bottom of code) and have tried placing/calling it throughout my code but am not sure exactly where to put it or if I can even do this with a function? If not, how can I achieve this?

  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
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Mamdlv
  • 540
  • 8
  • 23

3 Answers3

8

Create your own input function an add the desired behaviour:

def my_input(*args, **kwargs):
    str = input(*args, **kwargs)
    if str == "off":
        exit()
    return str

This will exit from the entire application when the user input is "off", to exit only from the function throw an exception instead, and catch it outside the function call. For instance:

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

and:

  def order_coffee():
      try:
          # your code
       except ValueError as err:
          # handle it accordingly
          return

A clearer approach is to create, throw, and handle a custom Exception. Have a look at this SO Thread to get an idea of how to implement those custom exceptions.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
1
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()
Seaver Olson
  • 450
  • 3
  • 16
1

You can create your own input function to check for this and when the event 'off' is entered then exit the program. That way if someone enters "off" at any point in the program it will end.

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()

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Roman Czerwinski
  • 527
  • 5
  • 16