0

i finished this coffee machine simulator but i have a small problem. How can i remove the line added by the check function after i enter the 'e' or 'l' or 'c'? Below is the code and a sample of the undesired output and the desired input.

The code:

import os
import time
menu = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee_l": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
    "money":0
}

def money():
    quarter=0.25
    dime=0.1
    nickel=0.05
    pennie=0.01
    qu=float(input("Inset quarters: "))
    di=float(input("Insert dimes: "))
    ni=float(input("Insert nickels: "))
    pen=float(input("Insert pennies: "))
    total_paid=qu*quarter+di*dime+ni*nickel+pen*pennie
    return total_paid

def check(drink):
    if drink == 'espresso':
        if resources['water'] >= 50 and resources['coffee'] >= 18:
            return ''
        elif resources['water'] <= 50:
            return "​Sorry there is not enough water."
        elif resources['coffee'] <= 18:
            return "​Sorry there is not enough coffee."
    elif drink=='latte':
        if resources['water'] >= 200 and resources['milk'] >= 150 and resources['coffee'] >= 24:
            return ""
        elif resources['water'] < 50:
            return "​Sorry there is not enough water."
        elif resources['coffee'] < 18:
            return "​Sorry there is not enough coffee."
        elif resources['milk'] < 150:
            return "​Sorry there is not enough milk."
            
    elif drink=='cappuccino':
        if resources['water'] >= 250 and resources['milk'] >= 100 and resources['coffee'] >= 24:
            return ""
        elif resources['water'] < 250:
            return "​Sorry there is not enough water."
        elif resources['coffee'] < 24:
            return "​Sorry there is not enough coffee."
        elif resources['milk'] < 100:
            return "​Sorry there is not enough milk."



def make_drink(drink):
    if drink == "espresso":
        resources['water']  -= 50
        resources['coffee'] -= 18
        resources['money'] += 1.5
    elif drink == "latte":
        resources['water']  -= 50
        resources['coffee'] -= 18
        resources['milk']   -= 150
        resources['money'] += 2.5
    elif drink == "cappuccino":
        resources['water']  -= 250
        resources['coffee'] -= 18
        resources['milk']   -= 100
        resources['money'] += 3
    else:
        print("Not sure what drink was made")
    return

def replenish():
    resources['water']  += 300
    resources['milk']   += 200
    resources['coffee'] += 100
    return

while True:
    print("Welcome to the coffee machine.")
    m="Espresso = 1.5$\nLatte = 2.5$\nCappuccino = 3.0$\n"
    print(m)
    choice=input("​What would you like? (espresso/latte/cappuccino)(e/l/c): ")
    if choice=='report':
        print(f"Water: {resources['water']}ml\nMilk: {resources['milk']}ml\nCoffee: {resources['coffee']}ml\nMoney: {round(resources['money'],2)}$")
    elif choice=='replenish':
        replenish()
    elif choice=='e':
        chk=check('espresso')
        print(chk)
        if chk=='':
            mo=money()
            if mo == menu['espresso']['cost']:
                make_drink('espresso')
                print('Here is your coffee')
            elif mo > menu['espresso']['cost']:
                make_drink('espresso')
                print(f'Here is your coffee, and here is your change {round(mo-menu["espresso"]["cost"], 2)} $')
            elif mo < menu['espresso']['cost']:
                print(f'​Sorry that\'s not enough money. Money refunded.')
    elif choice=='l':
        chk=check('latte')
        print(chk)
        if chk=='':
            mo=money()
            if mo == menu['latte']['cost']:
                print('Here is your coffee')
            elif mo > menu['latte']['cost']:
                print(f'Here is your coffee, and here is your change {mo-menu["latte"]["cost"]} $')
                resources['money']=mo+resources['money']
            elif mo < menu['latte']['cost']:
                print(f'​Sorry that\'s not enough money. Money refunded.')
    elif choice=='c':
        chk=check('cappuccino')
        print(chk)
        if chk=='':
            mo=money()
            if mo == menu['cappuccino']['cost']:
                print('Here is your coffee')
            elif mo > menu['cappuccino']['cost']:
                print(f'Here is your coffee, and here is your change {mo-menu["cappuccino"]["cost"]} $')
                resources['money']=mo+resources['money']
            elif mo < menu['cappuccino']['cost']:
                print(f'​Sorry that\'s not enough money. Money refunded.')
        
    elif choice=='off':
        break
    time.sleep(3)   
    os.system('clear')

The undesired output:

Welcome to the coffee machine.
Espresso = 1.5$
Latte = 2.5$
Cappuccino = 3.0$

What would you like? (espresso/latte/cappuccino)(e/l/c): e

Inset quarters: 

The desired output:

Welcome to the coffee machine.
Espresso = 1.5$
Latte = 2.5$
Cappuccino = 3.0$

What would you like? (espresso/latte/cappuccino)(e/l/c): e
Inset quarters: 

How can I do that without ruining the code? Thanks

  • Does this answer your question? [How to print without a newline or space](https://stackoverflow.com/questions/493386/how-to-print-without-a-newline-or-space) – Friedrich Feb 15 '23 at 15:30

2 Answers2

0

change the line

print(chk)

to

if chk:
    print(chk)

The empty line comes from calling check("expresso") and returning an empty line (no failure message to display)

So you only print chk if it has a message, but you don't print it if it's empty.

Sembei Norimaki
  • 745
  • 1
  • 4
  • 11
0

when check('espresso') exceuted, chk value became empty string. To solve it, you should validate if chk value is not an empty string and then print, else don't do anything. You can change the line :

print(chk)

with this:

print(chk) if chk else ''
Jordy
  • 1,802
  • 2
  • 6
  • 25