Quick Python Question

Started by
21 comments, last by Zahlman 17 years, 6 months ago
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.
Advertisement
llen = len(terrain)i = 0while i < llen:  print terrain  i = i + 1


This would be my solution.
for i in terrain:	print i
Thx!
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]
In X,Y Form: (Y_Position * Width_Of_Map) + X_Position

So, to access position (3,6) of your map, which is, say, 15 x 15 grid, you would use: (6 * 15) + 3. If you want to move west of that position, you would simply do (6 * 15) + 4. And you would obviously want to check that that is a valid position before trying to access it.
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:

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 = 0


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:
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
Of course.
That would be so much easier in general too.

This topic is closed to new replies.

Advertisement