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


Messages In This Thread
Referrencing before assignment problem - by martan45 - Jul-30-2017, 08:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with readlines() assignment Sunioj 5 4,922 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