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!
CodePudding user response:
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)
CodePudding user response:
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