(Python) 需要帮助格式化和整理代码以进行简单计算

发布于 2025-01-11 03:55:35 字数 2264 浏览 0 评论 0原文

我正在学习 Python,所以我对语法和格式的了解非常有限,但我希望有人能花时间帮助我完成这段代码。我正在尝试创建一个简单的程序来帮助我练习。这是我想做的事情的要点。

我正在尝试计算达到每项技能的最高级别(以经验指定)所需的时间,然后计算所有技能所需的总时间。

计算:每项技能的总小时数=(最大经验-我当前的经验)/每小时经验值

我想既然总共有23个技能,我希望我可以将它们制作成字典,在那里我可以提取它们的键和值,并且使用其他字典来存储其他值。这就是我开始的地方。 *注意,current_exp 的值“1”只是占位符,total_hours 是我想存储计算中每项技能的总小时数的位置。

max_exp = 13034431

current_exp = {
"Attack": 5574171,
"Strength": 13043713,
"Defence": 5388961,
"Range": 6179907,
"Prayer": 1900748,
"Magic": 6674380,
"Runecrafting": 687486,
"Construction": 3018183,
"Hitpoints": 10686667,
"Agility": 1342483,
"Herblore": 757889,
"Thieving": 13516382,
"Crafting": 1360543,
"Fletching": 5045556,
"Slayer": 2068113,
"Hunter": 1626552,
"Mining": 2515500,
"Smithing": 1347780,
"Fishing": 1362434,
"Cooking": 8670711,
"Firemaking": 5022270,
"Woodcutting": 1139504,
"Farming": 11122183,
}
est_hourly_exp = {
"Attack": 1,
"Strength": 1,
"Defence": 1,
"Range": 1,
"Prayer": 1,
"Magic": 1,
"Runecrafting": 1,
"Construction": 1,
"Hitpoints": 1,
"Agility": 1,
"Herblore": 1,
"Thieving": 1,
"Crafting": 1,
"Fletching": 150000,
"Slayer": 1,
"Hunter": 1,
"Mining": 1,
"Smithing": 1,
"Fishing": 1,
"Cooking": 225000,
"Firemaking": 1,
"Woodcutting": 1,
"Farming": 1,

}
total_hours = {
 "Attack": 1,
"Strength": 1,
"Defence": 1,
"Range": 1,
"Prayer": 1,
"Magic": 1,
"Runecrafting": 1,
"Construction": 1,
"Hitpoints": 1,
"Agility": 1,
"Herblore": 1,
"Thieving": 1,
"Crafting": 1,
"Fletching": 1,
"Slayer": 1,
"Hunter": 1,
"Mining": 1,
"Smithing": 1,
"Fishing": 1,
"Cooking": 1,
"Firemaking": 1,
"Woodcutting": 1,
"Farming": 1,  
}
#This is to compile them on a new line each time
[print(key,':',value) for key, value in current_exp.items()] 
#This is my calculation for a single skill
total_hours = (max_exp - current_exp["Fletching"]) / est_hourly_exp["Fletching"]
#I couldn't figure out how to add "total_hours" into the same print function as the one below
print("This is the total hours it would take with this skill:")
print(total_hours)

现在我相信有一百万种方法可以更轻松地做到这一点(可能是 for 循环,但我不理解它们),但我不完全确定如何去做。理想情况下,我想进行我的计算(或有效的计算)并存储我从每项技能中获得的每个单独值(例如“Fletching”的total_hours = 26.5),这样它就不会打印“current_exp”值,而是打印“current_exp”值。将打印每项技能的总小时数。

让它看起来漂亮是我可以独立完成的一件事,但更好地理解更精细的调整方法并使其发挥作用将不胜感激。感谢您抽出时间!

I'm getting into Python so my knowledge of syntax and formatting is very limited, but I was hoping someone would take the time to help me finish this code. I'm trying to create a simple program to help me practice. Here is the gist of what I am trying to do.

I'm trying to calculate how my hours it would take to reach the max level (designated in experience) for each skill, and then calculate the total hours it would take for them all.

The calculation: total hours per skill = (The max experience - my current experience) / exp per hour

I thought since there were 23 skills in total, I was hoping I could make them into dictionaries where I could pull their key and value, and use other dictionaries to store the other values. Here is where I started. *Note, the value "1" for current_exp are just placeholders, and total_hours is where I'd like to store the total hour per skill from the calculation.

max_exp = 13034431

current_exp = {
"Attack": 5574171,
"Strength": 13043713,
"Defence": 5388961,
"Range": 6179907,
"Prayer": 1900748,
"Magic": 6674380,
"Runecrafting": 687486,
"Construction": 3018183,
"Hitpoints": 10686667,
"Agility": 1342483,
"Herblore": 757889,
"Thieving": 13516382,
"Crafting": 1360543,
"Fletching": 5045556,
"Slayer": 2068113,
"Hunter": 1626552,
"Mining": 2515500,
"Smithing": 1347780,
"Fishing": 1362434,
"Cooking": 8670711,
"Firemaking": 5022270,
"Woodcutting": 1139504,
"Farming": 11122183,
}
est_hourly_exp = {
"Attack": 1,
"Strength": 1,
"Defence": 1,
"Range": 1,
"Prayer": 1,
"Magic": 1,
"Runecrafting": 1,
"Construction": 1,
"Hitpoints": 1,
"Agility": 1,
"Herblore": 1,
"Thieving": 1,
"Crafting": 1,
"Fletching": 150000,
"Slayer": 1,
"Hunter": 1,
"Mining": 1,
"Smithing": 1,
"Fishing": 1,
"Cooking": 225000,
"Firemaking": 1,
"Woodcutting": 1,
"Farming": 1,

}
total_hours = {
 "Attack": 1,
"Strength": 1,
"Defence": 1,
"Range": 1,
"Prayer": 1,
"Magic": 1,
"Runecrafting": 1,
"Construction": 1,
"Hitpoints": 1,
"Agility": 1,
"Herblore": 1,
"Thieving": 1,
"Crafting": 1,
"Fletching": 1,
"Slayer": 1,
"Hunter": 1,
"Mining": 1,
"Smithing": 1,
"Fishing": 1,
"Cooking": 1,
"Firemaking": 1,
"Woodcutting": 1,
"Farming": 1,  
}
#This is to compile them on a new line each time
[print(key,':',value) for key, value in current_exp.items()] 
#This is my calculation for a single skill
total_hours = (max_exp - current_exp["Fletching"]) / est_hourly_exp["Fletching"]
#I couldn't figure out how to add "total_hours" into the same print function as the one below
print("This is the total hours it would take with this skill:")
print(total_hours)

Now I'm confident there are a million ways to do this easier (probably a for loop, but I don't understand them), but I'm not entirely sure how to go about it. IDEALLY, I'd like to take my calculation (or one that works) and store each INDIVIDUAL value I get from each skill (ex. total_hours for "Fletching" = 26.5) so that instead of it printing the "current_exp" value, it would print the total amount of hours per skill.

Making it look nice is one thing I can work on independently, but getting a better understanding of a more fine tune approach and getting it to work would be appreciated. Thanks for your time!

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

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

发布评论

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

评论(2

橘虞初梦 2025-01-18 03:55:35

字典上的 for 循环会迭代字典中的所有键。因此,您可以一次检查一项技能的值,并一次计算一项技能的小时数。

然后,您可以使用 sum() 对所有小时数求和:

# max_exp, current_exp, and est_hourly_exp are the same as in the original question.
# They have been omitted for brevity.

skill_hours = {}
for skill in current_exp:
    skill_hours[skill] = (max_exp - current_exp[skill]) / est_hourly_exp[skill]

total_hours = sum(skill_hours.values())
print(skill_hours)
print(total_hours)

A for loop over a dictionary iterates over all of the keys in the dictionary. So, you can examine the values for one skill at a time and compute the number of hours for each skill one at a time.

Then, you can sum over all the number of hours using sum():

# max_exp, current_exp, and est_hourly_exp are the same as in the original question.
# They have been omitted for brevity.

skill_hours = {}
for skill in current_exp:
    skill_hours[skill] = (max_exp - current_exp[skill]) / est_hourly_exp[skill]

total_hours = sum(skill_hours.values())
print(skill_hours)
print(total_hours)
北方。的韩爷 2025-01-18 03:55:35

实际上,有一种非常好的方法可以使用 pandas 库来完成您想要做的事情,创建一个数据框,您可以在其中轻松提取有关数据的信息。要在同一行中打印结果,您应该这样做:

print("This is the total hours it would take with this skill: "+str(total_hours))

问题是您实际上无法以这种方式更改字典,因此当您编写 total_hours = (max_exp - current_exp["Fletching"]) / est_hourly_exp[ "Fletching"] 您正在将字典变量覆盖为浮点变量。

安装 pandas 后,您可以执行以下操作:

import pandas as pd

    df = pd.DataFrame(index=['Attack',
     'Strength',
     'Defence',
     'Range',
     'Prayer',
     'Magic',
     'Runecrafting',
     'Construction',
     'Hitpoints',
     'Agility',
     'Herblore',
     'Thieving',
     'Crafting',
     'Fletching',
     'Slayer',
     'Hunter',
     'Mining',
     'Smithing',
     'Fishing',
     'Cooking',
     'Firemaking',
     'Woodcutting',
     'Farming'], columns=['current_exp', 'est_hourly_exp', 'total_hours'])

然后分配您的值,例如:

ehexp = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 150000, 1, 1, 1, 1, 1, 225000, 1, 1, 1]

df['est_hourly_exp'] = ehexp

Actually there is a extremely better way to do what you're trying to do there with the pandas library, creating a dataframe where you can easily extract information about the data. To print the results in the same line you should do:

print("This is the total hours it would take with this skill: "+str(total_hours))

The problem is that you can't actually change a diciontary in that way, so when you write total_hours = (max_exp - current_exp["Fletching"]) / est_hourly_exp["Fletching"] you're overwriting your dictionary variable as a float variable.

Once you get pandas installed you could do:

import pandas as pd

    df = pd.DataFrame(index=['Attack',
     'Strength',
     'Defence',
     'Range',
     'Prayer',
     'Magic',
     'Runecrafting',
     'Construction',
     'Hitpoints',
     'Agility',
     'Herblore',
     'Thieving',
     'Crafting',
     'Fletching',
     'Slayer',
     'Hunter',
     'Mining',
     'Smithing',
     'Fishing',
     'Cooking',
     'Firemaking',
     'Woodcutting',
     'Farming'], columns=['current_exp', 'est_hourly_exp', 'total_hours'])

And then assign your values as the example:

ehexp = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 150000, 1, 1, 1, 1, 1, 225000, 1, 1, 1]

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