簡體   English   中英

Function 為每個人返回相同的值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM