🔒 Locked
Quick Python Question
Advertisement
Original Post
Can't believe Im asking this but: Some code: terrain = [ [ 1, 1, 1, 1, 1, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 1, 1, 1, 1, 1 ],] print terrain When it prints it prints like this [[1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1]] I could have sworn \ would make the it start prining on the next line. I want it to print out like this: [ 1, 1, 1, 1, 1, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 1, 1, 1, 1, 1 ] :( Edit: It screwed up.
New question.
It could be possible I am going about this wrong.
Heres what I have:
City = [[ 1, 1, 1, 1, 1, 1, 1], [ 1, 2, 0, 2, 0, 0, 1], [ 1, 0, 0, 0, 0, 0, 1], [ 1, 0, 0, 9, 0, 0, 1], [ 1, 0, 0, 0, 0, 0, 1], [ 1, 1, 1, 1, 1, 1, 1]]
llen = len(City)
i = 0
while i < llen:
print City
i = i + 1
What I need to know how to do is to be able to reference a specific spot on that. Preferbly in X,Y Form.
I want to be able to do this:
Pos = X,Y
Move = input("move")
if Move == W:
City(Pos) = 0
Pos = X,Y+1
City(Pos) = 9
elif Move == S:
City(Pos) = 0
Pos = X,Y-1
elif Move == A:
City(Pos) = 0
Pos = X-1,Y
elif Move == D:
City(Pos) = 0
Pos = X+1,Y
pos = City[X,Y]
It could be possible I am going about this wrong.
Heres what I have:
City = [[ 1, 1, 1, 1, 1, 1, 1], [ 1, 2, 0, 2, 0, 0, 1], [ 1, 0, 0, 0, 0, 0, 1], [ 1, 0, 0, 9, 0, 0, 1], [ 1, 0, 0, 0, 0, 0, 1], [ 1, 1, 1, 1, 1, 1, 1]]
llen = len(City)
i = 0
while i < llen:
print City
i = i + 1
What I need to know how to do is to be able to reference a specific spot on that. Preferbly in X,Y Form.
I want to be able to do this:
Pos = X,Y
Move = input("move")
if Move == W:
City(Pos) = 0
Pos = X,Y+1
City(Pos) = 9
elif Move == S:
City(Pos) = 0
Pos = X,Y-1
elif Move == A:
City(Pos) = 0
Pos = X-1,Y
elif Move == D:
City(Pos) = 0
Pos = X+1,Y
pos = City[X,Y]
City is a list. It just so happens to only contain lists. To access a specific element in a list contained by City, you must first subscript City to obtain the list in question, then subscript that list to obtain the element:
City = [[ ... ], [ ... ], ... ]# access coordinate pair (i, j)element = City[j]
Thx for the help, I got that working. I have a new question, do I have to list all my global variables that I want to use in a function?
For example:
It is annoying to keep having to say global X global Y in each of my functions, when I want to use variables X and Y in that function. Is there anyway I can get around having to list every variable each time?
I would much rather have my code look like this:
For example:
global Strglobal MHpglobal Hpglobal Agilglobal MMnglobal Mnglobal Mpglobal Gglobal Lvlglobal Xpglobal Skpglobal Fstglobal Equipedglobal Buffsglobal BuffsEnglobal Romeglobal Locglobal PosXglobal PosYdef warriorstart( ): global Str global MHp global Hp global Agil global MMn global Mn global Mp global G global Lvl global Xp global Skp global Fst global Equiped global Buffs global BuffsEn print "You have chosen the warrior!" Str = 18 Hp = 150 Mhp = 150 Agil = 10 MMn = 10 Mn = 10 Mp = 0 G = 10 Lvl = 1 Xp = 0 Skp = 1 Fst = 5 Equiped = [ 'W', 'A', 'S', 'T'] stats( )def stats( ): global Str global MHp global Hp global Agil global MMn global Mn global Mp global G global Lvl global Xp global Skp global Fst global Equiped global Buffs global BuffsEn print "Level,", Lvl print "Xp,", Xp print "Skill Points,", Skp print "Free Stat Points,", Fst print "Strength,", Str print "Agility,", Agil print "Magic Power,", Mp print "Health Points,", Hp print "Mana,", Mn print "Gold,", G print "Equiped:", Equiped print "Assign Stats: 1" print "Not now: 0" NowStats = input("Assign Stats Now?") if NowStats == 1: while NowStats == 1: print "Strength: 1" print "Agility: 2" print "Magic Power: 3" print "Mana: 4" print "Health Points: 5" print "None: 0" WhichStat = input("Which Stat") if WhichStat != 0: HowMuch = input("How Much") if HowMuch <= Fst: Fst = Fst - HowMuch if WhichStat == 1: Str = Str + HowMuch if WhichStat == 2: Agil = Agil + HowMuch if WhichStat == 3: Mp = Mp + HowMuch if WhichStat == 4: MMn = MMn + HowMuch Mn = Mn + HowMuch if WhichStat == 5: Hp = Hp + HowMuch MHp = MHp + HowMuch if WhichStat == 0: NowStats = 0It is annoying to keep having to say global X global Y in each of my functions, when I want to use variables X and Y in that function. Is there anyway I can get around having to list every variable each time?
I would much rather have my code look like this:
global Strglobal MHpglobal Hpglobal Agilglobal MMnglobal Mnglobal Mpglobal Gglobal Lvlglobal Xpglobal Skpglobal Fstglobal Equipedglobal Buffsglobal BuffsEnglobal Romeglobal Locglobal PosXglobal PosYdef warriorstart( ): print "You have chosen the warrior!" Str = 18 Hp = 150 Mhp = 150 Agil = 10 MMn = 10 Mn = 10 Mp = 0 G = 10 Lvl = 1 Xp = 0 Skp = 1 Fst = 5 Equiped = [ 'W', 'A', 'S', 'T'] stats( )def stats( ): print "Level,", Lvl print "Xp,", Xp print "Skill Points,", Skp print "Free Stat Points,", Fst print "Strength,", Str print "Agility,", Agil print "Magic Power,", Mp print "Health Points,", Hp print "Mana,", Mn print "Gold,", G print "Equiped:", Equiped print "Assign Stats: 1" print "Not now: 0" NowStats = input("Assign Stats Now?") if NowStats == 1: while NowStats == 1: print "Strength: 1" print "Agility: 2" print "Magic Power: 3" print "Mana: 4" print "Health Points: 5" print "None: 0" WhichStat = input("Which Stat") if WhichStat != 0: HowMuch = input("How Much") if HowMuch <= Fst: Fst = Fst - HowMuch if WhichStat == 1: Str = Str + HowMuch if WhichStat == 2: Agil = Agil + HowMuch if WhichStat == 3: Mp = Mp + HowMuch if WhichStat == 4: MMn = MMn + HowMuch Mn = Mn + HowMuch if WhichStat == 5: Hp = Hp + HowMuch MHp = MHp + HowMuch if WhichStat == 0: NowStats = 0
Why don't you use a global class with all your useful vars ?
Emmanuel
Emmanuel
Quote:
Original post by Emmanuel77
Why don't you use a global class with all your useful vars ?
Because that's horrible design? If you're working with an object-oriented language, your system should be modular. Each class should only know about the objects that it needs. Global variables = evil. Singletons used as a substitute for global variables = evil.
If anyone could link to information/tutorial on objects that would be great. I don't understand what they are.
Anyways, new Question.
What I am trying to do here with classes, is in the Arena function have En change depending on who your up against. Currently, I want each instance of En to be "Wolf" so that when it says En.Name, the program prints out Wolf, which is from Wolf.Name.
This works fine, except for
En.Attack( )
The error I get is this:
TypeError: unbound method Attack() must be called with Wolf instance as first argument (got nothing instead)
What I think that means is that the program wants me to use Wolf.Attack( ) instead of En.Attack. I would really much rather not do this.
So what should I do?
Anyways, new Question.
class Wolf: Name = 'Wolf' Hp = 50 Str = 10 Agil = 40 G = 2 Xp = 1 def Attack( ): print "The Wolf attacks dealing", Wolf.Str , "damage" Stat.Hp = Stat.Hp - Wolf.Str print "Your Hp now is", Stat.Hp def arena( ): move = 0 victory = 0 ranval = random.randint(0, 10) if ranval <= 7: En = Wolf print "You are in the Colosseum fightning a", En.Name Eny.Hp = En.Hp while move != 3: if En.Agil > Stat.Agil: En.Attack( ) print "[1] = Attack" print "[2] = Spell/Ability" print "[3] = Flee" move = input("Choose your move") if move == 1: print "You attack the", En.Name , "dealing", Stat.Str , "damage" Eny.Hp = Eny.Hp - Stat.Str if Eny.Hp < 0: Eny.Hp = 0 print "The ", En.Name , "health is", Eny.Hp if En.Agil <= Agil: En.Attack( ) if Eny.hp <= 0: move = 3 print "You killed the" , En.Name print "You gained", En.G , "Gold" Stat.G = Stat.G +En.G print "You gained", En.Xp , "Xp" Stat.Xp = Stat.Xp + En.XpWhat I am trying to do here with classes, is in the Arena function have En change depending on who your up against. Currently, I want each instance of En to be "Wolf" so that when it says En.Name, the program prints out Wolf, which is from Wolf.Name.
This works fine, except for
En.Attack( )
The error I get is this:
TypeError: unbound method Attack() must be called with Wolf instance as first argument (got nothing instead)
What I think that means is that the program wants me to use Wolf.Attack( ) instead of En.Attack. I would really much rather not do this.
So what should I do?
Quote:
Thanks
Arg, I changed the Def Attack( ): to Def Attack(self):
and I changed the En.Attack( ) to En.Attack(self)
Now I get
File "/Users/chris/Desktop/NewGame.py", line 103, in arena
En.Attack(self)
NameError: global name 'self' is not defined
When you call the method, don't provide the self argument. It is already the 'En' object that you call the method on.
define :
def Attack( self ):
And call
En.Attack()
Emmanuel
It sounds like you need to read Dive Into Python. Get through the objects section at least.
Quote:
Original post by drakostar
It sounds like you need to read Dive Into Python. Get through the objects section at least.
Thx
Gunna go through the whole thing.
I cant for the life of me get a function inside a class to work.
Heres the error I get.
File "/Projects/NewGame.py", line 108, in EnemyCheck
Wolf.Attack( )
TypeError: unbound method Attack() must be called with Wolf instance as first argument (got nothing instead)
def EnemyCheck( ):
global EnemyName
if EnemyName == Wolf.Name:
Wolf.Attack( )
Heres the error I get.
File "/Projects/NewGame.py", line 108, in EnemyCheck
Wolf.Attack( )
TypeError: unbound method Attack() must be called with Wolf instance as first argument (got nothing instead)
def EnemyCheck( ):
global EnemyName
if EnemyName == Wolf.Name:
Wolf.Attack( )
Sounds like Wolf is a class, not an object. You are trying to call a method without providing an object. This is probably closer to what you want:
Although, if you have an "EnemyName", I assume you also have an "Enemy" ... which should be an object (possibly of type 'Wolf') in the first place.
w = Wolf()w.Attack()Although, if you have an "EnemyName", I assume you also have an "Enemy" ... which should be an object (possibly of type 'Wolf') in the first place.
Name, Hp, Str, Agil, G, and Xp are currently defined as class variables. That means that the values will be shared between all Wolf objects. Since you probably want each Wolf object to have its own attributes, and you probably want them unique for each object. Something like this:
The __init__ method is called when a new object is created. I only implemented the name and hp attributes, so I'll leave the other variables as an exercise for you.
class Wolf: def __init__(self, name, hp): self.name = name self.hp = hp def attack(self): print "A wolf named", self.name, "with", self.hp, "hit points is attacking!"w = Wolf("Big Bad")w.Attack()The __init__ method is called when a new object is created. I only implemented the name and hp attributes, so I'll leave the other variables as an exercise for you.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement