簡體   English   中英

如何使此代碼循環進入字典而不繼續在 python 中循環?

[英]How do I make this code loop into dictionaries and not continue looping in python?

  • 編寫程序計算晚餐費用
    • 如果此人未滿 5 歲,晚餐免費
    • 如果此人未滿 10 歲,晚餐費用為 5 美元
    • 如果此人未滿 18 歲,晚餐費用為 10 美元
    • 如果這個人超過 65 歲,晚餐是 12 美元
    • 所有其他晚餐支付$ 15
    • 計算8%的稅
    • 顯示每位用餐者的總數、用餐者人數、每位用餐者的平均費用以及所有用餐者的累計總數
    • 程序應該循環直到進入退出,每個循環都會添加一個新的晚餐並更新總數

是我需要做的。 我不知道該怎么做,所以總 go 進入字典而不是循環。

我已經做了一個類似的問題,但現在這個問題需要把所有的金額加在一起,我不知道怎么做。 這是我為之前的類似問題所做的代碼。 我目前似乎遇到的問題是它不會將 go 放入字典中,並且不會在最后打印出來。 我還需要它繼續循環,直到我輸入退出。

dinner = {}
total ={}


name = input("What's your name? ")
age = input("What age is the person eating? ")
age = int(age)
amount = input("How many people that age? ")
amount = int(amount)

while True:
    if name == 'quit':
        print('Done')
        break
    elif age < 5:
        price = 0 * amount
        tax = price * 0.08
        dinner[name] = name
        dinner[age] = age
        total[amount] = price + tax
        break
    elif age < 10:
        price = 5 * amount
        tax = price * 0.08
        dinner[name] = name
        dinner[age] = age
        total[amount] = price + tax
        break
    elif age < 18:
        price = 10 * amount
        tax = price * 0.08
        dinner[name] = name
        dinner[age] = age
        total[amount] = price + tax
        break
    elif age > 65:
        price = 12 * amount
        tax = price * 0.08
        dinner[name] = name
        dinner[age] = age
        total[amount] = price + tax
        break
    else:
        price = 15 * amount
        tax = price * 0.08
        dinner[name] = name
        dinner[age] = age
        total[amount] = price + tax
        break
print("Thank you for having dinner with us! \nYour total is {total}, for {dinner}.")

最簡潔的方法是對年齡進行分類,確定給定年齡屬於哪個分類,然后使用索引返回價格。

  • np.digitize完成了這項任務
    • 參數bins包含年齡,並確定給定值適合列表中的哪個索引。 bins是獨占的,因此范圍是 0-4、5-9、10-17、18-65 和 66+。
    • 范圍對應於索引 0、1、2、3 和 4。
  • idx用於返回每個年齡段對應的價格
  • 使用 function 來返回成本,而不是一堆if-elif語句
  • 黃色方向,不要求返回人的姓名或年齡。
  • 最后打印所需的所有內容都可以通過維護成本清單來計算,其中包含每個客戶的價格。
  • print(f'some string {}')是一個f-String
  • 類型提示用於函數中(例如def calc_cost(value: int) -> float: )。
import numpy as np

def calc_cost(value: int) -> float:
    prices = [0, 5, 10, 15, 12]
    idx = np.digitize(value, bins=[5, 10, 18, 66])
    return prices[idx] + prices[idx] * 0.08


cost = list()

while True:
    age = input('What is your age? ')
    if age == 'exit':
        break
    cost.append(calc_cost(int(age)))

# if cost is an empty list, nothing prints
if cost:
    print(f'The cost for each diner was: {cost}')
    print(f'There were {len(cost)} diners.')
    print(f'The average cost per diner was: {sum(cost)/len(cost):.02f}')
    print(f'The total meal cost: {sum(cost):.02f}')

Output:

What is your age?  4
What is your age?  5
What is your age?  9
What is your age?  10
What is your age?  17
What is your age?  18
What is your age?  65
What is your age?  66
What is your age?  exit
The cost for each diner was: [0.0, 5.4, 5.4, 10.8, 10.8, 16.2, 16.2, 12.96]
There were 8 diners.
The average cost per diner was: 9.72
The total meal cost: 77.76

如果不允許使用numpy

def calc_cost(value: int) -> float
    return value + value * 0.08

cost = list()

while True:
    age = input("What age is the person eating? ")
    if age == 'exit':
        break
    age = int(age)
    if age < 5:
        value = 0
    elif age < 10:
        value = 5
    elif age < 18:
        value = 10
    elif age < 66:
        value = 15
    else:
        value = 12

    cost.append(calc_cost(value))

if cost:
    print(f'The cost for each diner was: {cost}')
    print(f'There were {len(cost)} diners.')
    print(f'The average cost per diner was: {sum(cost)/len(cost):.02f}')
    print(f'The total meal cost: {sum(cost):.02f}')

筆記:

  • 不要在所有if-elif條件中使用break ,因為這會破壞while-loop
  • 任何時候你重復一些事情,比如計算價格,寫一個 function。
  • 熟悉python 數據結構,如listdict
  • 這個需要把所有的金額加在一起,我不知道怎么做。
    • list.append(price)在循環中
    • sum(list)得到總數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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