【问题标题】:How do I make this code loop into dictionaries and not continue looping in python?如何使此代码循环进入字典而不继续在 python 中循环?
【发布时间】:2019-10-22 00:00:16
【问题描述】:
  • 编写程序计算晚餐费用
    • 如果此人未满 5 岁,晚餐免费
    • 如果此人未满 10 岁,晚餐 5 美元
    • 如果此人未满 18 岁,晚餐 10 美元
    • 65 岁以上,晚餐 $12
    • 所有其他晚餐费用为 15 美元
    • 计算 8% 税
    • 显示每位用餐者的总数、用餐者人数、每位用餐者的平均费用以及所有用餐者的累计总数
    • 程序应该循环直到进入退出,每个循环都会添加一个新的晚餐并更新总数

是我需要做的。我不知道该怎么做,所以总数会进入字典而不是循环。

我已经做过一个类似的问题,但现在这个问题需要将所有金额加在一起,我不知道如何。这是我为之前的类似问题所做的代码。我目前似乎遇到的问题是它不会进入字典,最后也不会打印出来。我还需要它继续循环,直到我输入退出。

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}.")

【问题讨论】:

  • 为什么是你的总数的关键,而不是用餐者的名字?您对 dicts 的预期最终结果是什么?为什么你要求在循环之外输入,这意味着你只能得到一个晚餐?为什么要在 每个 分支上中断,从而使 while 循环过时?

标签: python python-3.x loops dictionary input


【解决方案1】:

最简洁的方法是对年龄进行分类,确定给定年龄属于哪个分类,然后使用索引返回价格。

  • np.digitize 完成该任务
    • 参数bins,包含年龄,并确定给定值适合列表中的哪个索引。 bins 是独占的,因此范围是 0-4、5-9、10-17、18-65 和 66+。
    • 范围对应于索引 0、1、2、3 和 4。
  • idx 用于返回每个年龄段对应的价格
  • 使用函数返回成本,而不是一堆if-elif 语句
  • 黄色方向,不要求返回人的姓名或年龄。
  • 最后打印所需的所有内容都可以通过维护成本清单来计算,其中包含每个客户的价格。
  • print(f'some string {}')f-String
  • Type Hints 用于函数中(例如def calc_cost(value: int) -&gt; 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
  • 每当您重复某些事情(例如计算价格)时,请编写一个函数。
  • 熟悉python data structures,如listdict
  • 这个需要把所有的金额加在一起,我不知道怎么做。
    • list.append(price) 在循环中
    • sum(list)获取总数

【讨论】:

    猜你喜欢
    • 2021-02-06
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2014-03-11
    相关资源
    最近更新 更多