I don't know what is wrong! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I don't know what is wrong! (/thread-1240.html) Pages:
1
2
|
I don't know what is wrong! - William_the_Conqueror - Dec-16-2016 I was trying to code a dice game but i cant seem to get the goomba to take damage. I would appreciate some help. import random monNum = 1 y = 0 hp = 10 def attack(): diceValue = random.randint(1, 10) hp = hp - diceValue while(y==0): if(monNum==1): print("A goomba has crossed your path!") print() command = input("What will you do?:") print() if(command == "help"): print("attack") print() elif(command == "attack"): attack() elif(command == "dice"): print(diceValue) print() else: print() else: print()
RE: I don't know what is wrong! - Mekire - Dec-16-2016 You are trying to modify a global within a function. Consider this illegal (it is possible but highly discouraged so I'm not showing it). Make attack take hp as an argument and return the new hp instead. So your function could look like this: def attack(hit_points): diceValue = random.randint(1, 10) return hit_points - diceValueAnd could be invoked like this: hp = attack(hp) RE: I don't know what is wrong! - wavic - Dec-16-2016 Hello! The problem is in hp = hp - diceValue . What does happen? First, Python gets the right side of the equation, calculate it and then it's assigned to the left side. But hp variable is not defined. It actually is but as a global variable. Into a function all variables are local ones. If you want to use a global variable you have to tell that to Python explicitly.hp = 10 def attack(): global hp # here you tell Python that you want to use a global variable diceValue = random.randint(1, 10) hp = hp - diceValue RE: I don't know what is wrong! - Mekire - Dec-16-2016 (Dec-16-2016, 08:31 PM)Mekire Wrote: Consider this illegal (it is possible but highly discouraged so I'm not showing it). (Dec-16-2016, 08:37 PM)wavic Wrote: If you want to use a global variable you have to tell that to Python explicitly.Gonna resist the urge to edit your post; but please don't teach new programmers this. RE: I don't know what is wrong! - wavic - Dec-16-2016 Wouldn't you tell why? RE: I don't know what is wrong! - William_the_Conqueror - Dec-16-2016 Thanks wavic, it works perfectly now. RE: I don't know what is wrong! - sparkz_alot - Dec-16-2016 Two other things. First, you also have this elif (command == "dice"): print(diceValue) print()but 'diceValue' is not available here. Perhaps add it here? return hit_points - diceValue, diceValuewhich will return a tupple, which you can access. The second thing, and not a game changer, but you don't need the parenthesis in you're while, if's and elif's RE: I don't know what is wrong! - Mekire - Dec-16-2016 (Dec-16-2016, 08:43 PM)wavic Wrote: Wouldn't you tell why?This is exactly why. (Dec-16-2016, 08:45 PM)William_the_Conqueror Wrote: Thanks wavic, it works perfectly now.Now he thinks this is the way you should do this instead of doing it the correct way and learning. RE: I don't know what is wrong! - wavic - Dec-16-2016 I am just pointing the error. I even didn't read the rest of the code. RE: I don't know what is wrong! - Mekire - Dec-16-2016 Quote:I am just pointing the error. I even didn't read the rest of the code. The OP is just getting started using functions and you taught him the exact wrong thing to do. You should consider that. When shown my answer and yours he picked the one that he thinks is easiest which is not going to do him any favors in the future. This is like teaching someone to just exec a string when building a calculator. Yes it works. It is still wrong and not at all helpful. |