首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数为每个人返回相同的值。

函数为每个人返回相同的值。
EN

Stack Overflow用户
提问于 2022-08-28 22:09:44
回答 2查看 66关注 0票数 0

我正在为学校做一个学习Python的项目。在这个项目中,你坐在一家餐馆里,你给账单编程。当我计算每个人在函数中花费的总和并返回值时。我只收到每个人最后一个人的总数。

如果运行此代码并使用以下值:

代码语言:javascript
复制
2
tim
4
4
james
12
12

你会发现他们总共得到了69英镑。我想,如果我按下"calculate_amount“后面的一次选项卡将其放入for循环中,您会看到它实际上为其他人计算了正确的金额,但不知何故,当您将其从for循环中删除时,它会覆盖它。

我想要的是,在每个人告诉我他们吃了多少饮料和蛋糕之后,我得到了一份最后的报告,上面有每个人的总数。就像我现在做的一样,但是有正确的总数。

我希望有人能把我引向正确的方向,谢谢

代码语言:javascript
复制
#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.
EN

回答 2

Stack Overflow用户

发布于 2022-08-29 02:50:29

使用calculate_amount()函数,您没有在购买饮料的人中进行分析,因此该函数无法打印购买饮料的人,因此它只需多次打印给定金额的值。

相反,您可以尝试创建一个使用名称、饮料和蛋糕变量的函数,并为每个人调用该函数:

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2022-08-29 07:34:51

为了计算每个人花费的总金额,我使用了python字典。在字典里,我储存了名字,蛋糕的数量和饮料的数量。

在将相关信息存储在字典中之后,我将字典解析为一个函数,然后打印出每个人的选项卡。

代码语言:javascript
复制
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))

希望这会有所帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73522622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档