Home > Net >  Function returns same value for each person
Function returns same value for each person

Time:08-29

I'm working on a project for school to learn Python. In this project you are sitting at a restaurant and you program the bill. When I calculate the total of what everyone spend in a function and return the values. I only receive the total of the last person for every person.

If you run this code and use these values:

2
tim
4
4
james
12
12

you will see that they both get 69 in total. I figured out if I press one time tab behind the "calculate_amount" to put it in the for loop you see it actually calculates the amounts correctly for the other people, but somehow it overwrites it when you put it out of the for loop

What I want is that after everyone told me how many drinks and cake they ate, I get a final report with all the totals of each person. Like I have right now but then with the correct totals.

I hope someone can steer me in the right direction thanks

#function makes first a calculation of the total each person has spend

def calculate_amount(amount_drinks, amount_cake):
    count_number = 0
    totaalFris = amount_drinks * drink_price
    total_cake = amount_cake * cake_price
    totaal = total_cake   totaalFris
    totaal = str(totaal)

    # then a for statement to print for each person the amount they have spend
    for person in list_names:
        count_number  = 1
        print(str(count_number)   " "   person   " "   str(totaal))


#prices of the food
drink_price = 2.50
cake_price = 3.25


#lists
list_names = []
drinked_sodas = []
eaten_food = []
count = 0

#while loop to check if there are coming people between 2-6
while True:
    person_amount = int(input("How many people are coming? (2-6) "))
    if person_amount < 2 or person_amount > 6:
        print("error")
    else: break


# ask for every person coming what their name, drinks and food is. and add that to the lists
for persoon in range(person_amount):
    naam_persoon = str(input('\nWhat is your name? '))
    list_names.append(naam_persoon)

    glasses = int(input("How many drinks did you had? "))
    drinked_sodas.append(glasses)

    cake = int(input("how much cake did you eat? "))
    eaten_food.append(cake)
    count  = 1

#calling the function which calculates the amount of glasses and cakes everyone had
#if u you put the function in this for statement it kinda works(press one time tab)

    calculate_amount(glasses, cake)



#problem is I get same total amount for every person.

CodePudding user response:

With the calculate_amount() function you have not parsed in the people buying drinks, therefore the function could not print the people that have bought the drinks so it just prints the value of the amount given multiple times.

Instead, you could try to create a function that takes the name, drinks, and cake variables and call the function for each person:

drink_price = 2.50
cake_price = 3.25 

def singular_tab(name, glasses, cakes):
    total_drinks = glasses * drink_price
    total_cakes = cakes * cake_price
    total = total_drinks   total_cakes
    return print(f'name: {name}, tab: {total}')
   
while True:
    person_amount = int(input("How many people are coming? (2-6) "))
    if person_amount < 2 or person_amount > 6:
        print("error")
    else: break


for persoon in range(person_amount):
    naam_persoon = str(input('\nWhat is your name? '))
    glasses = int(input("How many drinks did you had? "))
    cake = int(input("how much cake did you eat? "))
    singular_tab(naam_persoon, glasses, cake)

CodePudding user response:

To calculate the total amount of money spent by each person I utilized python dictionaries. With the dictionaries, I stored the name, amount of cakes, and amount of drinks.

After storing the relevant information in the dictionary I then parsed the dictionary into a function which then printed out each person's tab.

drink_price = 2.50
cake_price = 3.25
people_data = []

def tab_amount(dict):
    final = []
    final_text = ""
    for people in dict:
        cake_total = int(people[2]) * cake_price
        glass_total = int(people[1]) * drink_price
        person_total = cake_total   glass_total
        final.append([people[0], person_total])
        person_total = 0
    for totals in final:
        final_text  = f'name: {totals[0]} spent in total: {totals[1]} euro\n'
    return final_text

        
        

while True:
    person_amount = int(input("How many people are coming? (2-6) "))
    if person_amount < 2 or person_amount > 6:
        print("error")
    else: break


for person in range(person_amount):
    name = str(input('\nWhat is your name? '))
    glasses = int(input("How many drinks did you had? "))
    cake = int(input("how much cake did you eat? "))
    people_data.append([name, glasses, cake])
    
print(tab_amount(people_data))

Hope this helps :)

  • Related