-3

I'am new to python, and i decided to make a simple quiz game. The idea is: Your base health is 100, every wrong answered question is -25 hp. If hp equals 0 you lose.

And here lies the problem. I don't know how to pass the variable from function to a second function (in this case, every question in quiz is a different function)

The problem is that in every function, hp resets to its basic number (100). Sorry if i poorly described my problem but im not very fluent in english.

Already tried to make a function that contains ,,hp = 100", making it global etc. Also tried various stuff with ,,return".

hp = 100

def test1():
    test = input("yes no")
    if test == "yes":
        print("this is hp test")
        print(hp - 25) # should be 100 - 25 = 75
        test2()

    if test == "no":
        print("ok")
        input("dead end")

def test2():
    test2 = input("yes no")
    if test2 == "yes":
        print("this is second hp test")
        print(hp - 25) # should be 75 - 25 = 50 
    if test2 == "no":
        print("ok")
        input("another dead end")

input("start")
test1()
Thomas Thorogood
  • 2,150
  • 3
  • 24
  • 30
nkr
  • 7
  • even though it is not a good practice, you can use `global hp` inside each function, and modify its value. – lmiguelvargasf Sep 19 '19 at 21:06
  • If you search in your browser for "Python function pass value", you'll find references that can explain this much better than we can manage here. – Prune Sep 19 '19 at 21:11
  • It seems to me that you're expecting `print(hp - 25)` to change the value stored in `hp`. To do that, you have to assign a new value to `hp`, something like `hp = hp - 25` – Steve Archer Sep 19 '19 at 21:28

4 Answers4

1

Use global inside each function declartion

hp = 100

def test1():
    global hp # do this in each of your functions
    test = input("yes no")
    if test == "yes":
        print("this is hp test")
        hp -= 25 # which is equivalent to hp = hp - 25
        print(hp) # here just print the updated variable
        test2()

Keep in mind that using global variables is not considered a good practice because it might make your code very hard to debug. You can read more about it here.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • will using global cause `print(hp - 25)` to change the value of `hp`? I think that's the issue that the asker is having. – Steve Archer Sep 19 '19 at 21:26
  • @SteveArcher, no, it won't because you are just printing the variable, you need to add somewhere `hp = hp - 25` if what you want is to decrease that value by 25 or the shortcut `hp -= 25` which is basically the same. – lmiguelvargasf Sep 19 '19 at 21:28
  • yeah, exactly, so I think advising the use of using global misses the crux of the asker's issue here. They're not having trouble with the scope of `hp`, they're not understanding how variables are assigned values – Steve Archer Sep 19 '19 at 21:30
  • 1
    @SteveArcher, I have updated my answer. Now it addresses more appropriately the question. – lmiguelvargasf Sep 19 '19 at 21:43
1

I am not really sure what your trying to achieve here. But i would suggest using a class that will allow you to have better control over your variables.

class Game:
  def __init__(self):
      self.hp = 100

  def takeInput(self):
      self.current = input()
      self.computeScore()

  def computeScore(self):
      if self.input ==="Something":
         self.hp -= 25
      if self.checkValidScore():
            self.takeInput()
      else:
         print "game over"

  def checkValidScore(self):
      return self.hp < 0
Yatish Kadam
  • 454
  • 2
  • 11
  • 1
    Notice that this answer uses `-=` to SET the value of the variable. In the question, you're just printing the value of `hp - 25` you're not actually changing the value of `hp` – Steve Archer Sep 19 '19 at 21:25
1

The statement print(hp - 25) simply prints the value of hp minus 25. It does not actually modify the value of hp. You probably want:

hp = hp - 25
print(hp)
Thyrus017
  • 43
  • 2
  • 11
0

Not sure what you want to achieve. If it keeps that simple you could also go in the following direction...

def test1(hp):
    test = input("yes no")
    stop_msg = None
    if test == "yes":
        print("not ok")
        hp -= 25
    elif test == "no":
        print("ok")
        stop_msg = "dead end"
    else:
        raise Exception("Expected input to be 'yes' or 'no'.")
    return hp, stop_msg


def test2(hp):
    test = input("yes no")
    stop_msg = None
    if test == "yes":
        print("'yes' sucks")
        hp -= 25
    elif test == "no":
        print("ok")
        stop_msg = "another dead end"
    else:
        raise Exception("Expected input to be 'yes' or 'no'.")
    return hp, stop_msg


def run_game(hp=100):
    print("start...")
    tests = [test1, test2]
    for test in tests:
        hp, stop_msg = test(hp)
        print("hp: {}".format(hp))
        if stop_msg:
            print(stop_msg)
            return


if __name__ == "__main__":
    run_game()

Remarks:

  • If you want to implement a more complex decision tree, you could use any simple tree representation.
  • If you have always the same structure within testX functions, introduce one function with parameters for questions, answer, etc.
matheburg
  • 2,097
  • 1
  • 19
  • 46