0

I am trying to make a text based game using the def tool to make an easy loop. However i am unable to have it when the player dies, the player is asked if they wish to play again, and the loop restarts. I have changed the code so that when the player dies, playAgain = "n" so the loop ends but it still does nothing. is it because it's a global variable and i'm trying to affect it with a local variable? If so how could i fix it?

playAgain = "y"
while playAgain == "y" or playAgain == "yes" :
    displayIntro()
    choosePath()
    checkPath()
    NextSteps()
    Reaction()
    attackCHOICE()

    if playAgain != "y" or playAgain != "yes":
        time.sleep(1)
        print()
        delayprint("Do you want to play again? ")
        playAgain = input()

  • I this all inside a function? could you pleas show a bit more of the code structure – Guinther Kovalski Feb 13 '20 at 18:31
  • if you simple run: ```import time playAgain = "y" while playAgain == "y" or playAgain == "yes" : #displayIntro() #choosePath() ##checkPath() #NextSteps() #Reaction() #attackCHOICE() if playAgain != "y" or playAgain != "yes": time.sleep(1) print() #delayprint("Do you want to play again? ") playAgain = input() ``` it will have the exactly behavior you are expecting. This means that you may have a context variable problem, as you said, or that maybe one of your functions are doing something they should not – Guinther Kovalski Feb 13 '20 at 18:34
  • It's just in a "while" loop – Midget Cowz Feb 13 '20 at 18:39

3 Answers3

0

When we create a variable inside a function it is local to that function. Consider the code below.

playAgain = "y"  # Define a variable outside the function

def setPlayAgainToNo():
    playAgain = "no"


print(playAgain)  # value is "y"
setPlayAgainToNo()  # This modifies a local variable and not the global one
print(playAgain)  # value is still "y"

Note that if we define the function below

def printPlayAgain():
    print(f"The value of playAgain is {playAgain}")

then it will print the value of the variable defined outside because we didn't create a local "playAgain" variable.

Ok, then how do I modify the global value inside a function?

Just use global variableName inside your function to tell python to NOT create a local variable and use the one from outside.

def setPlayAgainToNo():
    global playAgain
    playAgain = "no"  # this will modify the global value
darcamo
  • 3,294
  • 1
  • 16
  • 27
  • Thank you for the global variable advise, however in my code that is not shown when the player dies i set the variable into #playAgain = "n" which will work now with #global variableName so thank you – Midget Cowz Feb 13 '20 at 18:44
0
playAgain = "y"
while playAgain == "y" or playAgain == "yes" :
    displayIntro()
    choosePath()
    checkPath()
    NextSteps()
    Reaction()
    attackCHOICE()

    delayprint("Do you want to play again? ")
    playAgain = input()

    if playAgain != "y" or playAgain != "yes":
        time.sleep(1)
        print()

Simply move your play again check outside of your if statement.

Your current loop runs forever because initially, playAgain is always "y" so it will never reach your if statement and ask if they want to play again.

alex067
  • 3,159
  • 1
  • 11
  • 17
  • This won't work because when the user picks `n`, the program will sleep then start the game before asking the user if he wants to play again or no. The desired behavior is to keep asking till the user says yes then the game should start, not before. – helcode Feb 13 '20 at 18:45
  • This will work 100%. If the user picks n, then your if statement will execute, and the loop will stop. The game definitely won't loop again. – alex067 Feb 13 '20 at 18:52
  • but the ask is to sleep for (1) and then offer the user to play again. At least this is how the question is phrased. – helcode Feb 13 '20 at 21:21
  • then just move the sleep command above your input? I mean that is such a minuscule thing that doesn't even solve his issue – alex067 Feb 13 '20 at 21:57
0

There are two problems with your code.

  1. You never collect input from the user except inside the if statement (which never triggers because playAgain is always y)
  2. playAgain won't change because of user's choice in any other function (e.g. inside displayIntro() or displayIntro()) because, in Python, arguments are passed by assignment, in other words, changing a variable inside a function won't change it in the outer scope.

A greate answer explaining assignments in python can be found here

To get around both issues, you need to assign the function return value to the outer scope variable playAgain or seek the user's input out of the if block.

I have modified your code below to highlight the second approach.

playAgain = "y"
while playAgain == "y" or playAgain == "yes" :
    displayIntro()
    choosePath()
    checkPath()
    NextSteps()
    Reaction()
    attackCHOICE()
    playAgain = input()


    if playAgain != "y" or playAgain != "yes":
        time.sleep(1)
        print()
        delayprint("Do you want to play again? ")
        playAgain = input()
helcode
  • 1,859
  • 1
  • 13
  • 32