1

Feel free to edit this post if it requires it.

Hi, I have been learning python for two months now, and I just finished learning about functions and blue bean logic. I decided to try my hand at making a text adventure on the side while I am not in class, and seeing how it turns out at the end of the semester. My tutor is not available this week, or the next, so I take my questions here. I feel like my problem has to do with me not fully understanding how the global variables work yet, as I just started using them, or if I should even use them to store data for this purpose. The idea I have with this game is essentially to run on calling a bunch of functions that I will make when I need them to, because I feel like it is pretty efficient for what I know. Running this code gives the error "positional argument is missing" if you are to run it. What could I do better here? I tried to integrate a function to test it out too, so that it would be easier to give me assistance.

tldr; how can I make the experience points system I am trying to envision work so that it saves the number of experience points that are gained, then adds that number to the new higher requirement of experience for the next level as in most RPG games? For example, upon adding five experience, the program errors out. Thanks in advance for reading this! I hope I adequately explained my issue.

here is the code that I have come up with so far

global playername
playername = "Name Jeff 21 Bruh"
global playerlvl
playerlvl = 0
global xpcurrent
xpcurrent = 0
global xpnext
xpnext = int(((playerlvl * 4) / 3) + 4)
global xpgained
xpgained = 0

def show_stats():
        print("*" * 20, "\n")
        print(playername, "the Adventurer")
        print("Lvl", playerlvl)
        print("EXP =", xpcurrent, '/', xpnext)
        print("-_" * 16 + '\n')

def lvlup(xpgained):
    global xpcurrent
    xpgainedhere = xpgained
    xpcurrent += xpgained
    xpgained = 0
    if xpcurrent >= xpnext:
        playerlvl + 1
        xpcurrent -= xpnext
        print("~ ~", playername, "is Lvl", playerlvl, "! ~ ~")
        if xpcurrent >= xpnext:
            lvlup()
        else:
            show_stats()
    else:
        print("~ ~", playername, "gained", xpgainedhere, "Exp! ~ ~")
        show_stats()

def game_main():
        print("*" * 50)
        print("This is where the game begins.")
        # ------just before this \n (the '.' )is the character limit
        # ---------------------------------------------------------V
        print("The player after lines of text can enter commands.\n"
              "use this to enter commands to test them.")
        print("*" * 50, "\n")

        def testexp():
            addexp = int(input("type the integer for how much exp "
                               "that you would like to add\n"))
            lvlup(addexp)

        testexp()
        testexp()
        testexp()
        testexp()
        testexp()
        testexp()
        testexp()
        testexp()

game_main()
Σamuel
  • 11
  • 3

1 Answers1

0

Global variables are generally a bad idea. Try to convert your functions to use parameters instead of global variables.

this part

def show_stats():
        print("*" * 20, "\n")
        print(playername, "the Adventurer")
        print("Lvl", playerlvl)
        print("EXP =", xpcurrent, '/', xpnext)
        print("-_" * 16 + '\n')

would become

def show_stats(playername, playerlvl, xpcurrent, xpnext):
        print("*" * 20, "\n")
        print(playername, "the Adventurer")
        print("Lvl", playerlvl)
        print("EXP =", xpcurrent, '/', xpnext)
        print("-_" * 16 + '\n')

to use this new function instead of making all the variables global do

playername = "Name Jeff 21 Bruh"
playerlvl = 0
xpcurrent = 0
xpnext = int(((playerlvl * 4) / 3) + 4)
show_stats(playername, playerlvl, xpcurrent, xpnext)
Glitch__
  • 304
  • 1
  • 10
  • 1
    Thanks for the tip, may I ask why global variables are a bad idea? I am still pretty new, and I would like a reason not to use them. Anyway, I think that that would make sense though. I could just declare all the variables outside of my main loop and then the functions would use the variables as arguments, correct? – Σamuel Oct 13 '21 at 05:47
  • @Σamuel using global variables can become messy quite quickly. For example: you have a global variable named ` string = "teststring" ` if a function then uses a local variable (variable defined in a function and only accessible from within the function) with the same name the ` string ` variable will be modified. Thus it's usually better to not use global variables and just pass variables as parameters. – Glitch__ Oct 13 '21 at 09:34