-1
initial_p = input("Enter the initial point")

def game():
    x = 1
    guess = input("Guess value")
    if guess == 1:
        initial_p += 2
    else:
        initial_p -= 2

game()

replay = raw_input("Do you want to try it again? Y/N")
if replay == 'Y':
    game()

each game needs 2 points

I made it really simple just to explain this stuff easily

So to play each game, it requires you to have at least 2 points otherwise it becomes game over if you guess right, you earn 2 points if not, you lose 2 points.

with the outcome(points), you can either play again or quit

if you play again, you pay two points

HOWEVER, when you play for the second time or more, that line

initial_p += 2 and initial_p -= 2 still have points that you typed in the very beginning

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
SMLJ
  • 9
  • 4
  • Please correctly format your post. Apart from that, I think you are having problem understanding scope of the variables and not their names. `initial_p` appears to be a global variable currently but is accessed improperly. – Vivek Rai Nov 23 '14 at 06:43
  • Possible dupe or related to [Using global variables in a function other than the one that created them](http://stackoverflow.com/q/423379/2823755). Another possible answer - http://stackoverflow.com/a/370363/2823755. And http://stackoverflow.com/a/146365/2823755 – wwii Nov 23 '14 at 06:46
  • http://stackoverflow.com/a/292502/2823755. And finally from the Tutorial in the docs https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces – wwii Nov 23 '14 at 06:53
  • There are many problems with this code, and there is no coherent question. – Karl Knechtel Sep 11 '22 at 08:19

1 Answers1

0

The quick and dirty response is to change to the following.

def game(initial_p):
    #Your Code
    return initial_p


initial_p = game(initial_p)

Basically, you're placing the global variable as a local variable and reassigning the global.

This should also happen at the very bottom as well.

Also, you can just ask for the input inside of the function, and have a default argument of initial_p.

For example,

def game(first_time=True)
    #Ask for input with if first_time:
    return initial_p

And modify some global point value or something from the return.

Sorry if this is sloppy, was written on my mobile.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
AdriVelaz
  • 543
  • 4
  • 15