-2

So I'm in an intro to programming class and I need help updating the score in my "text based adventure game". What happens is you press enter, then a text appears (ex, you walk to the plane to find a monkey), then it says "Your score is 5". I want it so every time I press enter, the text appears and then my score goes up by 5. I spent a couple hours reading through my textbook and I don't know what to do. Everything is fine except the showScore function since it keeps printing my score as 5 after every step.

This is an example of the code:

def promptUser():

    input("\n<Press Enter to continue...>\n")

def base():

    print("You approach the base with friends")

def showScore():

    x = 0
    score = x + 5
    print("Your score is now" ,score,)
ham ham
  • 11
  • 2
  • 1
    Yes, you are resetting x to 0 every time to run showScore – jwebb Sep 18 '18 at 20:49
  • How can I fix this? I have been trying to fix this for so long I don't even know what else to do. Don't know why people are downvoting this thread...just asking for some assistance... – ham ham Sep 18 '18 at 20:52
  • To answer your question every time you are calling showScore, you are setting `x` back to zero, and then back to `5` you aren't keeping it to add 5. – MooingRawr Sep 18 '18 at 21:05

3 Answers3

1

I'm not a python programmer but I came across you post while doing some peer review for first timers questions and I thought I could help. Global Variables is what will solve your issue and based on this Python Global Variabl explaination the following should work for you.

score = 0

def promptUser():

    input("\n<Press Enter to continue...>\n")

def shelter():

    print("You approach the base with friends")

def showScore():

    score = score + 5
    print("Your score is now" ,score)

Currently you are defining your the variable 'x' within the showScore() function which means each time you call that function you are resetting x to zero before you add 5. In my solution, I define score as a Global variable so that each time you call the showScore() function it accepts simply adds 5 to score and then saves that in memory as the new score before displaying the score.

This may work for your current problem but in the real world it would be better to have a function that is dedicated to changing the score that is separate from the function that displays the score.

Hope this helps.

Azeame
  • 2,322
  • 2
  • 14
  • 30
  • Then python is dumping its memory between executions and you will need to find out how to persist data to a database or file between executions. Try starting here [Easiest way to persist a data structure to a file in python? -- StackOverflow](https://stackoverflow.com/questions/1047318/easiest-way-to-persist-a-data-structure-to-a-file-in-python) – Azeame Sep 18 '18 at 22:19
0

Try using:

def showScore():
   score = 0
   score = score + 5
   print('Your score is now',score)
  • this does the same thing as his function. – MooingRawr Sep 18 '18 at 21:07
  • like mooing said, it does the same thing as my function, it keeps showing 5 is my score. Would someone mind helping? I can't seem to understand this even though I read through my textbook multiple times – ham ham Sep 18 '18 at 21:16
0

did you try putting score = 0 before the function then putting global score before score = score +5?

  • 2
    In order to make this a useful answer, please provide a code example of what you are suggesting, rather than just a description of what the code would be. – WOUNDEDStevenJones Mar 09 '21 at 19:51