Jul-30-2017, 08:42 PM
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()