[英]How do I make 'continue' statement iterate the “mother” loop outside of loop where the statement is found? (Python)
[英]How do I make this code loop into dictionaries and not continue looping in python?
是我需要做的。 我不知道該怎么做,所以總 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+。idx
用於返回每個年齡段對應的價格if-elif
語句print(f'some string {}')
是一個f-Stringdef 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}')
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
list
和dict
list.append(price)
在循環中sum(list)
得到總數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.