[英]Function returns same value for each person
我正在为学校做一个项目来学习 Python。 在这个项目中,你坐在一家餐馆里,你对账单进行编程。 当我计算每个人在 function 中花费的总额并返回值时。 我只收到每个人最后一个人的总数。
如果您运行此代码并使用这些值:
2
tim
4
4
james
12
12
你会看到他们都得到了 69。 我发现如果我按“calculate_amount”后面的一个时间选项卡将其放入 for 循环中,您会看到它实际上为其他人正确计算了金额,但是当您将其从 for 循环中取出时,它会以某种方式覆盖它
我想要的是,在每个人告诉我他们吃了多少饮料和蛋糕之后,我得到一份包含每个人总数的最终报告。 就像我现在有的那样,但是总数是正确的。
我希望有人能引导我朝着正确的方向前进谢谢
#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.
使用 calculate_amount() function 您还没有解析购买饮料的人,因此 function 无法打印购买饮料的人,因此它只会打印多次给出的金额的值。
相反,您可以尝试创建一个 function 以获取名称、饮料和蛋糕变量,并为每个人调用 function:
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)
为了计算每个人花费的总金额,我使用了 python 字典。 用字典,我存储了名字、蛋糕的数量和饮料的数量。
将相关信息存储在字典中后,我将字典解析为 function,然后打印出每个人的标签。
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))
希望这可以帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.