-1

Basically I’m making a choose your own adventure type game, and in the beginning you invest points into skills like the beginning of Fallout. I successfully made the point system function and I wanted to make sure the points would translate out of the function instead of being local. So as a test, I made it so if asked to lift weights, and input ‘Lift’ if strength above 17 it would print you lifted it, if below it would print weak

Strength = 0
Perception = 0
Luck = 0
agility = 0

print("This is the skill tree, whichever abilities you invest in shall change the story and what you are able to do and how you solve situations and fight. Strength allows one to wield bigger weapons, solve situations with brute force, and will make fights easier. Perception allows secrets, chests, and many secret items to be found, and solutions ones with lesser perception might not spot otherwise. Luck allows certain situations to be bypassed, enemies to miss, and is the wild card of the abilities. Agility allows to bypass situations using acrobatics, and is useful in every situation. Create your character how you want, as long as the points add up to 20    \
    ")
def Point_sys(Strength,Perception, Luck, agility ):
    P_all= Strength + Perception + Luck + agility
    if P_all>20 or P_all<20:
        display(HTML('<h1>Error! make sure all the points add up to 20</h1>'))
    if P_all == 20:
        display(HTML('<h1>Character correctly made, loading game<h1> '))

    story(Strength,Perception, Luck,agility )

interact_manual(Point_sys, Strength=widgets.IntSlider(min=0, max=20,steps=1,value = 0),                                    Perception=widgets.IntSlider(min=0, max=20,steps=1, value=0), Luck=widgets.IntSlider(min=0, max=20,steps=1, value=0), agility= widgets.IntSlider(min=0,max=20,steps=1, value=0))


#Problems, my strength is not translating from my point system code, need to make my attributes global
#giving choice before interact_manual
def story(Strength, Perception,Luck,agility):
    choice=input("lift weight")
    if choice == 'Lift' and Strength >= 17:
        print("you lifted the weight")
    if choice != 'Lift':
        print ("you left")
    if choice == 'Lift' and Strength<17:
        print('weak ')

story(Strength, Perception, Luck, agility)

I want it so if someone makes their character, say with 20 strength, it allows them to do certain stuff later when i program the story. So should someone have 20 strength, they can lift weight, below 17 however, they cant lift weight. However, the point system and weight lift test are 2 different functions, as I will replace the weight test with my whole story code. I've tried specifying the input as an integer, but that gives a value error stating invalid literal for int() with base 10: 'Lift' and without specifying as an integer it resulted in an EOL error when reading line. I've also tried try except.

Ignatius Reilly
  • 1,594
  • 2
  • 6
  • 15
  • EOL error is not related to global variables. Using `try - except` blocks to avoid errors is a bad practice: the code is not working anyway, but you see the problem a lot later and is more difficult to debug. Why would you set the input to be an integer, how would this be related to your problem? – Ignatius Reilly Jun 07 '23 at 02:31
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Ignatius Reilly Jun 07 '23 at 02:32
  • Please, try to generate a [mre] of your problem. We can't reproduce it without knowing, for example, what `interact_manual` is doing (And probably we don't need to know it). – Ignatius Reilly Jun 07 '23 at 02:38
  • Also, try to make your style consistent. E.g. if the function `story` is in lowercase, make `Point_sys` in lowercase too. Also, if you follow the [most accepted conventions](https://peps.python.org/pep-0008/), it makes easy to understand your code. Your future yourself will thank you. – Ignatius Reilly Jun 07 '23 at 02:43
  • This would potentially be a good scenario to learn some basic OOP. Make a class for the player character, and store the stats as attributes of the class. All of the functions for manipulating the statistics would be methods of the class. Then you just have 1 global variable - an instance of the Player class. – nigh_anxiety Jun 08 '23 at 00:04

2 Answers2

0

The common way to make Python treat Strength, and other variables as global is not to include them in the parameters list of the function "story" and "Point_sys" :

Strength = 0
Perception = 0
Luck = 0
agility = 0

def story():
    choice=input("lift weight")
    if choice == 'Lift' and Strength >= 17:
        print("you lifted the weight")
    if choice != 'Lift':
        print ("you left")
    if choice == 'Lift' and Strength<17:
        print('weak ')

def Point_sys():
    P_all= Strength + Perception + Luck + agility
    if P_all>20 or P_all<20:
        display(HTML('<h1>Error! make sure all the points add up to 20</h1>'))
    if P_all == 20:
        display(HTML('<h1>Character correctly made, loading game<h1> '))
    story()

def interact_manual():
    Strength = widgets.IntSlider(min=0, max=20,steps=1,value = 0)
    Perception = widgets.IntSlider(min=0, max=20,steps=1, value=0) 
    Luck = widgets.IntSlider(min=0, max=20,steps=1, value=0) 
    agility = widgets.IntSlider(min=0,max=20,steps=1, value=0)
    Point_sys() 

interact_manual()
belamy
  • 44
  • 6
  • In Python, calling a function with a global immutable integer like Strength as parameter, with the hope of changing that global immutable integer within a function (I assume that's what the interact_manual function tried to do and display in Point_sys), would not work. – belamy Jun 07 '23 at 04:35
  • It is because the OP decided to use the 4 global variables at the outset. And the simplest way to make his piece of code work is what I suggested - to not put global parameters in his function definition. It is clear that the change to his 4 global variables are being done in his interact_manual() function, where the player use some kind of GUI slider to change the values. See the code modified. – belamy Jun 07 '23 at 16:58
  • The variables defined in `interact_manual` are now in a higher level than the call to `Point_sys()`, but they're not technically global. It could fix the problem. But it should be stated to avoid bugs. Specially because the real global variables with those names don't make sense anymore. – Ignatius Reilly Jun 07 '23 at 17:01
  • Since the OP did not provide code for the interact_manual() function, I put the prototype code there just as an example for the OP to figure out the rest based on the same concept. – belamy Jun 07 '23 at 17:10
0

I think the problem is coming from the logic operators. you should have elif and an else statement. if marks the beginning of an if then, elif would be the next. and then else would be the final logic operator. So you should have

def story(Strength, Perception,Luck,agility):
    choice=input("lift weight")
    if choice == 'Lift' and Strength >= 17:
        print ("you lifted the weight")
    elif choice != 'Lift':
        print ("you left")
    else choice == 'Lift' and Strength < 17:
        print ('weak ')

    story(Strength, Perception, Luck, agility)