对每个输入中的值求和

发布于 2025-01-13 19:54:01 字数 701 浏览 2 评论 0原文

我试图获取存储在字典中的每个变量的总值。我是一个初学者,正在为这行代码而苦苦挣扎。有人可以帮我找出这个错误吗?

income_list = { 
    "Job": [],
    "Business": [],
    "Dividends": [],
    "Sideline": [],
    "Others": []

}
total_income = 0
job = income_list["Job"].append(float(input(f"Enter income for job: € ")))
business = income_list["Business"].append(float(input(f"Enter income for Business: € ")))
dividends = income_list["Dividends"].append(float(input(f"Enter income for Dividends: € ")))
sideline = income_list["Sideline"].append(float(input(f"Enter income for Sideline: € ")))
others = income_list["Others"].append(float(input(f"Enter other incomes: € ")))

for value in income_list.values():
 print(sum(value)) 

I am trying to get the total value of each variable that's stored in the dictionary. I am a beginner who's struggling with this line of code. Can someone help me figure out the bug?

income_list = { 
    "Job": [],
    "Business": [],
    "Dividends": [],
    "Sideline": [],
    "Others": []

}
total_income = 0
job = income_list["Job"].append(float(input(f"Enter income for job: € ")))
business = income_list["Business"].append(float(input(f"Enter income for Business: € ")))
dividends = income_list["Dividends"].append(float(input(f"Enter income for Dividends: € ")))
sideline = income_list["Sideline"].append(float(input(f"Enter income for Sideline: € ")))
others = income_list["Others"].append(float(input(f"Enter other incomes: € ")))

for value in income_list.values():
 print(sum(value)) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缱绻入梦 2025-01-20 19:54:01

如果您需要总和,您可以使用列表理解:

total = sum([sum(source) for source in income_list.values()])

即使您的收入来源包含多个值(因为它们是列表),此方法也将起作用。

通过为 sum 提供生成器可以实现相同的结果:

total = sum(sum(source) for source in income_list.values())

You can use a list comprehension, given that you need a sum of sums:

total = sum([sum(source) for source in income_list.values()])

This method will work even if your income sources contain multiple values (since they are lists).

The same result can be achieved by giving a generator to sum:

total = sum(sum(source) for source in income_list.values())
舞袖。长 2025-01-20 19:54:01

你可以尝试这样的事情:

from itertools import chain

sum(chain(income_list.values())

you could try something like this :

from itertools import chain

sum(chain(income_list.values())
坚持沉默 2025-01-20 19:54:01

我认为你想要以下两件事之一:1)带有字典/对象的列表,或2)带有列表的字典。我会选择 1),因为它对我来说更直观,但由于您似乎已经在尝试实现 2),所以我也会这样做。

我将提供一个简约的示例,然后您应该能够将其扩展到您的程序。

my_lists = {
  "list1": [],
  "list2": []
}

my_input = Input()
my_lists["list1"].append(my_input)

for x in my_lists["list1"]:
  print(x)

I think you want one of two things: 1) a list with dictionaries/objects, or 2) a dictionary with lists. I would go with 1) because it feels more intuitive to me, but since it seems you are already trying to achieve 2), I will do that as well.

I will provide a mimimalistic example, and then you should be able to extend it to your program.

my_lists = {
  "list1": [],
  "list2": []
}

my_input = Input()
my_lists["list1"].append(my_input)

for x in my_lists["list1"]:
  print(x)
盛夏尉蓝 2025-01-20 19:54:01

首先,如果您要存储的是单个值,则使用列表作为字典值是没有意义的。其次,您需要在 for 循环中添加“total_venue”变量。

该代码有效:

income_list = { 
}

total_income = 0
income_list["Job"]=float(input(f"Enter income for job: € "))
income_list["Business"]=float(input(f"Enter income for Business: € "))
income_list["Dividends"]=float(input(f"Enter income for Dividends: € "))
income_list["Sideline"]=float(input(f"Enter income for Sideline: € "))
income_list["Others"]=float(input(f"Enter other incomes: € "))

for value in income_list.values():
    total_income += value

print(total_income)

也就是说,我认为您甚至不需要为此使用字典,只需简单的浮点变量即可。

First, it makes no sense to use a list as the dictionary value if what you want to store is a single value. Second, you need to add to the "total_income" variable in the for loop.

That code works:

income_list = { 
}

total_income = 0
income_list["Job"]=float(input(f"Enter income for job: € "))
income_list["Business"]=float(input(f"Enter income for Business: € "))
income_list["Dividends"]=float(input(f"Enter income for Dividends: € "))
income_list["Sideline"]=float(input(f"Enter income for Sideline: € "))
income_list["Others"]=float(input(f"Enter other incomes: € "))

for value in income_list.values():
    total_income += value

print(total_income)

That said, I think you don't even need to use a dictionary for that, just simple float variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文