[PYCHARM]need help figuring out how to "send" a stat to be used in a different Def
Pycharm is just a regular python editor, so for future reference you don't need to specify it.
The interesting part here is what you're doing in the code that calls those functions. At a guess based on what you're saying, I think that code looks like
healthStat = 0 armorStat = 0 damageStat = 0 totalStat = healthStat + armorStat + damageStat createCustom(healthStat, armorStat, damageStat, totalStat) beginning(healthStat, armorStat, damageStat)
In python (and most languages), each variable is scoped to the function that it lives in, and when you pass it to another function it creates a copy*. To solve that, you have to store the results (which you're already returning from the function) back into the variables, which you would do with something like
healthStat = 0 armorStat = 0 damageStat = 0 totalStat = healthStat + armorStat + damageStat healthStat, armorStat, damageStat, totalStat = createCustom(healthStat, armorStat, damageStat, totalStat) beginning(healthStat, armorStat, damageStat)
You never read the values of the input to createCustom though, so you don't need to be passing those as arguments. That would make the code look like
healthStat, armorStat, damageStat, totalStat = createCustom() beginning(healthStat, armorStat, damageStat)
In createCustom, you just remove the arguments from the top of the function, so it looks like
def createCustom(): print("...")
You also need to do something similar with the error case when the user enters the wrong value. You can assign the results to all of the variables again, so it becomes
if totalStat != 20: print("...") healthStat, armorStat, damageStat, totalStat = createCustom()
but if you do that then it will run the next line, where you print out a summary of their stats, once for each time they entered the values wrongly. You can fix that, and make the code neater, by immediately returning the result of createCustom:
if totalStat != 20: print("...") return createCustom()
* it's a bit more subtle than this in practice, because if you pass an array then changes to the array will be reflected in the original array too. What is really not propagated to the caller is assignment, where you do myVar = something, so in-place modification (like myVar[0] = something) still works.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now