Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't know what is wrong!
#3
Hello! The problem is in hp = hp - diceValue. What does happen? First, Python gets the right side of the equation, calculate it and then it's assigned to the left side. But hp variable is not defined. It actually is but as a global variable. Into a function all variables are local ones. If you want to use a global variable you have to tell that to Python explicitly.

hp = 10
def attack():
    global hp # here you tell Python that you want to use  a global variable

    diceValue = random.randint(1, 10)
    hp = hp - diceValue
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: I don't know what is wrong! - by Mekire - Dec-16-2016, 08:31 PM
RE: I don't know what is wrong! - by wavic - Dec-16-2016, 08:37 PM
RE: I don't know what is wrong! - by Mekire - Dec-16-2016, 08:40 PM
RE: I don't know what is wrong! - by wavic - Dec-16-2016, 08:43 PM
RE: I don't know what is wrong! - by sparkz_alot - Dec-16-2016, 08:47 PM
RE: I don't know what is wrong! - by Mekire - Dec-16-2016, 08:51 PM
RE: I don't know what is wrong! - by wavic - Dec-16-2016, 08:57 PM
RE: I don't know what is wrong! - by Mekire - Dec-16-2016, 09:05 PM
RE: I don't know what is wrong! - by nilamo - Dec-16-2016, 09:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,704 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 3,071 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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