Python Forum
Referrencing before assignment problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Referrencing before assignment problem
#1
As you can see from the code I am a beginner. When I run this code, the error is that g_heal is referenced before it is assigned a value. This is untrue because g_heal should have a random value between 25 and 40. Even when I take this away, I have the same problem with g_def. Can anyone help me out with this please? Would be greatly appreciated. Also there is code before this but that is just assigning values to your character.


a1 = int(input("You are on your way to the castle when you see a guard, he is alone and looks weak. To fight type 1, to leave him type 2. Note: Fighting increases your attributes but decreases your energy by 1. Energy = 20: "))

def fight():
    print(g_heal)
    p_q = ((a_p / 100) * mob) - (g_def / 3)
    m_q = ((a_p / 100) * mob * 1.45) - (g_def / 3)
    h_q = ((a_p / 100) * mob * 1.8) - (g_def / 3)
    you = int(input("""
    Type 1 for quick hit. Damage =""", p_q, """ Chance of hitting = 100%
    Type 2 for medium hit. Damage =""", m_q, """ Chance of hitting = 70%
    Type 3 for hard hit. Damage =""", h_q, """Chance of hitting = 70%"""))
    hitper = random.randint(1,10)
    if hitper > 0 and you == 1:
        g_heal = g_heal - p_q
    if hitper > 3 and you == 2:
        g_heal = g_heal - m_q
    if hitper > 6 and you == 3:
        g_heal = g_heal - h_q
    if hitper < 4 and you == 2:
        print("YOU MISSED!")
    if hitper < 7 and you == 3:
        print("YOU MISSED!")
    print("The guards health is", g_heal)
    if g_heal <= 0:
        battle = False
        a_p = a_p + 2
        defe = defe + 2
        mob = mob + 2
        print("All attributes increased by 2!")
    hitme = (g_a_p / 2) - (defe // 10)
    heal = heal - hitme
    print("The guard dealt", hitme," damage and now you have", heal," health")
    if heal < 1:
        print("YOU ARE DEAD!")

if a1 == 1:
    battle = True
    g_a_p = random.randint(20,35)
    g_def = random.randint(10,25)
    g_heal = random.randint(25,40)
    while battle == True:
        fight()
Reply
#2
The name g_heal is assigned on module level (global) to an object. You can use methods inside your function on this object. But you can't assign inside the function a new value to a global name. You can do this, but then you've to use the statement global variable_name inside your function before any assignment happens. But then you're doing it wrong. This leads to strange errors in your program.

def foo():
    print('Read access from foo', a)
    # this refers to a, which is assigned on
    # global scope

def bar():
    global a
    print('Read access from bar', a)
    a = 1337
    print('Written to a inside bar:', a)


a = 42
foo()
bar()
print('A on global scope:', a) # a has changed to 1337
Instead of using global, you should use following way:
def bar(variable):
    result = variable * 2
    variable = result ** 2
    return result, variable

a = 42
result, new_a = bar(a)
print('A:', a, 'Result:', result, 'New_a', new_a)
There you don't assign a new object to a, you have still the old value assigned to a.
You've the result and the name new_a, which you can call with other functions, maybe together
with a. Depends what you need.

In general a function should not have side effects. A side effect is for example changing a name outside of a function.
A function has it's own scope, which lives in the stack. After leaving the function, the stack of the function is lost.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with readlines() assignment Sunioj 5 4,754 Oct-27-2019, 06:20 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020