I am writing a code for a school project, but i can't get it to work. #Magic The Gathering Life Tracker Life_P1 = 20 #All players start with 20 life while (Life_P1 > 0): intput = input() def Add_Life(): print("Player 1:", Life_P1 + 1) def Lose_Life(): print("Player 1:", Life_P1 - 1) #These add or subtract life
if input == "+": Add_Life() if input == "-": Lose_Life() #These take the input and activate the add/lose funtions. (I swear it's indented. don't know why it wont show/)
Only thing I find odd about that is that you're defining functions within the while loop? I would do it more like this... Although I'm not familiar with Python, and I'm no programming pro by any means, but this seems a little better to me:
Also notice that I've defined the functions before the while loop. I'm not sure if this is good practice or what, like I said, I'm no python pro and I haven't even run it myself. I'd be amazed if it does work to be honest, but there we go, I also prefer the no whole "-=" and the "+=" to what @Denson did, but it does the exact same thing, I'm lazy, less typing.
Traceback (most recent call last): File "python", line 18, in File "python", line 8, in Lose_Life UnboundLocalError: local variable 'Life_P1' referenced before assignment Definitely an interesting implementation, almost worked
@Devon_Cahill, try this, it actually worked and ran on my machine, so I'd assume it'll work for you?
Feel free to spice things up a bit and what not, I've tried out different kinds of programming from functional to OOP to logical, etc. I'm no pro, but from learning how to do things the way I have learned how to do them, I like this implementation personally.
But I have no background with Python and I don't know if this is a good implementation or if this is a dirty implementation, all I know is that it works.
There's a few things a saw that you might want to change. The only thing in your while loop is intput = input() which is misspelled but since it's not indented correctly it will just loop until you cancel the program as it will never add or remove life. The second thing is you should not rely on global variables unless they are static. If you don't use a class you need to pass the variable as a parameter. When you are running a script by itself it's also good to use "if name == "main": ". I didn't get that type of input to work for me so I used raw_input where you can also add a comment.
Oh, I see, sorry, I'm a bit dopey, I should've assumed that's what you meant. Global just takes in the global variable, in this case, the global variable is Life_P1, if this were in Java or some other language I'm much more confident with we'd call it a class variable. I assume that without the keyword global, the function/method does not have complete access to the variable, it can't actually change the global variable.
The keyword global is only useful to change or create global variables in a local context, although creating global variables is seldom considered a good solution.
You know a variable is a global/class variable when it's defined at the top of the code and it's outside of a function/method, when a variable is defined within a function/method it becomes a local variable. It's okay to use class/global variable, but from my experience, I've been taught that it's bad practice to use a lot of global/class variables, unless you need them.
As I've said, I'm no Python pro, I don't know how Python compiles code and I don't know how to make some advanced or complex applications, what I've stated is 110% educated assumption. I could be talking pure BS for all I know, but judging from how it works and my knowledge, I think I'm somewhat, to some extent correct.
Your solution is probably more suitable and more acceptable than mine, I don't know any of the specific coding conventions that's applicable to Python.