Home > Software design >  Python code input show again in the return
Python code input show again in the return

Time:01-26

This code is written in Python 3 and working fine at a glance. Problem is when input is given and it fill the condition, after that the input show again. For example, when "Vegetar" input is given 3 times its fulfil the condition by return this message "Sorry, no more left of 'Vegetar' today." but after when again "Vegetar" is given in input its execute "What would you like today?". As it it finished it should say "Sorry, no more left of 'Vegetar' today." again.

items = [ 'Dagens', 'Vegetar', 'Halal' ]
prices = [ 53.90, 42.50, 59.90 ]
number = [1, 1, 1]
meal_sum = 0
total_sum = 0
dagens_sum = 0
vegetar_sum = 0
halal_sum = 0

def sell(item):
    global meal_sum
    global total_sum
    global dagens_sum
    global vegetar_sum
    global halal_sum
    if item == 'Dagens':
        meal_sum  = number[0]
        total_sum  = prices[0]
        dagens_sum  = number[0]
        print("Dagens, here you go.\n")
    elif item == 'Vegetar':
        meal_sum  = number[1]
        total_sum  = prices[1]
        vegetar_sum  = number[1]
        print("Vegetar, here you go.\n")
    elif item == 'Halal':
        meal_sum  = number[2]
        total_sum  = prices[2]
        halal_sum  = number[2]
        print("Halal, here you go.\n")
    else:
        print("%s is not on the menu!\n" % item)

def dagens():
    global dagens_sum
    dagens_sum = 0
    print("Sorry, no more left of 'Dagens' today.")   

def vegetar():
    global vegetar_sum
    vegetar_sum = 0
    print("Sorry, no more left of 'Vegetar' today.")
    
def halal():
    global halal_sum
    halal_sum = 0
    print("Sorry, no more left of 'Halal' today.")
      
def empty_cash_registry():
    global meal_sum
    meal_sum = 0
    print("Cash registry emptied. Thieves be warned!\n")

def cafeteria():
    global meal_sum
    global total_sum
    print("What would you like today? (type 'how is business?' to attempt small talk, or 'not hungry' to stop)")
    user_input = input("> ")
    while user_input != 'not hungry':
        question = 0
        if user_input == 'how is business?':
            question = 1
            if total_sum == 0:
                print("Not good so far. No one seems to be hungry today!\n")
            elif total_sum < 500:
                print("Alright. Could have been better\n")
            elif total_sum > 500:
                print("Excellent! Lots of hungry students around today.\n")
        if question == 0:
            sell(user_input)
        if dagens_sum > 15:
            dagens()
        if vegetar_sum > 2:
            vegetar()
        if halal_sum > 5:
            halal()
        if meal_sum > 200:
            empty_cash_registry()
        print("What would you like today? (type 'how is business?' to attempt small talk, or 'not hungry' to stop)")
        user_input = input("> ")
    print("Welcome back later!\n")

if __name__ == '__main__':
    cafeteria()

CodePudding user response:

Don't check for the limit after calling sell(). Put the limit checks inside the sell() function, and don't allow them to buy more if the items is over the limit.

items = [ 'Dagens', 'Vegetar', 'Halal' ]
prices = [ 53.90, 42.50, 59.90 ]
number = [1, 1, 1]
meal_sum = 0
total_sum = 0
dagens_sum = 0
dagens_max = 15
vegetar_sum = 0
vegetar_max = 2
halal_sum = 0
halal_max = 5

def sell(item):
    global meal_sum
    global total_sum
    global dagens_sum
    global vegetar_sum
    global halal_sum
    if item == 'Dagens':
        if dagens_sum > dagens_max:
            print("Sorry, no more left of 'Dagens' today.")
            return
        meal_sum  = number[0]
        total_sum  = prices[0]
        dagens_sum  = number[0]
        print("Dagens, here you go.\n")
    elif item == 'Vegetar':
        if vegetar_sum > vegetar_max:
            print("Sorry, no more left of 'Vegetar' today.")
            return
        meal_sum  = number[1]
        total_sum  = prices[1]
        vegetar_sum  = number[1]
        print("Vegetar, here you go.\n")
    elif item == 'Halal':
        if halal_sum > halal_max:
            print("Sorry, no more left of 'Halal' today.")
            return
        meal_sum  = number[2]
        total_sum  = prices[2]
        halal_sum  = number[2]
        print("Halal, here you go.\n")
    else:
        print("%s is not on the menu!\n" % item)

def empty_cash_registry():
    global meal_sum
    meal_sum = 0
    print("Cash registry emptied. Thieves be warned!\n")

def cafeteria():
    global total_sum
    print("What would you like today? (type 'how is business?' to attempt small talk, or 'not hungry' to stop)")
    user_input = input("> ")
    while user_input != 'not hungry':
        question = 0
        if user_input == 'how is business?':
            question = 1
            if total_sum == 0:
                print("Not good so far. No one seems to be hungry today!\n")
            elif total_sum < 500:
                print("Alright. Could have been better\n")
            elif total_sum > 500:
                print("Excellent! Lots of hungry students around today.\n")
        if question == 0:
            sell(user_input)
        if meal_sum > 200:
            empty_cash_registry()
        print("What would you like today? (type 'how is business?' to attempt small talk, or 'not hungry' to stop)")
        user_input = input("> ")
    print("Welcome back later!\n")

if __name__ == '__main__':
    cafeteria()
  • Related