Python for loop breaks text based game
Question: 
    #!/usr/bin/env python
    
    import random
    import time
    import os
    
    class vars:
        running = 1
    
    def win ():
        print("You escaped!")
        vars.running = 0
        time.sleep(4)
        return 0
    
    def main ():
        char_loc = 11 #The characters current co-ordinates in XY format
        pos_char_loc = 11
        ex_y = random.randint(1, 5)
        ex_x = random.randint(1, 5) * 10
        ex_loc = ex_x + ex_y
    
        while vars.running == 1:
            os.system('CLS')
            x0 = ["#"] * 5
            x1 = ["#"] * 5
            x2 = ["#"] * 5
            x3 = ["#"] * 5
            x4 = ["#"] * 5
            if (char_loc >= 11  and char_loc <= 55):
                if (char_loc >= 11 and char_loc <= 15):
                    i = 0; k = 11
                    for x in range(0, 4):          
                        if char_loc == k:
                            x0.insert(i, '@')
                        else:
                            i += 1
                            k += 1 
                if (char_loc >= 21 and char_loc <= 25):
                    i =0; k = 21
                    for loop1 in range(0, 4):
                        if char_loc == k:
                            x1.insert(i, '@')
                        else:
                            i += 1
                            k += 1
                if (char_loc >= 31 and char_loc <= 35):
                    i =0; k = 31
                    for loop2 in range(0, 4):
                        if char_loc == k:
                            x2.insert(i, '@')
                        else:
                            i += 1
                            k += 1                                
                if (char_loc >= 41 and char_loc <= 45):
                    i =0; k = 41
                    for loop3 in range(0, 4):
                        if char_loc == k:
                            x3.insert(i, '@')
                        else:
                            i += 1
                            k += 1
                if (char_loc >= 51 and char_loc <= 55):
                    i =0; k = 51
                    for loop5 in range(0, 4):
                        if char_loc == k:
                            x4.insert(i, '@')
                        else:
                            i += 1
                            k += 1
                else:
                    print("fail")
    
            print( x0[4],x1[4],x2[4],x3[4],x4[4])
            print( x0[3],x1[3],x2[3],x3[3],x4[3])
            print( x0[2],x1[2],x2[2],x3[2],x4[2])
            print( x0[1],x1[1],x2[1],x3[1],x4[1])
            print( x0[0],x1[0],x2[0],x3[0],x4[0])
            print(char_loc, ex_loc)
            if char_loc == ex_loc:
                win()         
            move = input()
            if move == "w" and (char_loc != 15 and char_loc != 25 and char_loc != 35 and char_loc != 45 and char_loc !=55):
                char_loc += 1
                print("up")
            elif move == "s" and (char_loc != 11 and char_loc != 21 and char_loc != 31 and char_loc != 41 and char_loc != 51):
                char_loc -= 1
                print("down")
            elif move == "a" and (char_loc != 11 and char_loc != 12 and char_loc != 13 and char_loc != 14 and char_loc != 15):
                char_loc -= 10
                print("left")
            elif move == "d" and (char_loc != 51 and char_loc != 52 and char_loc != 53 and char_loc != 54 and char_loc != 55):
                char_loc += 10
                print("right")
            else: print("You can't move there!")
    
    if __name__ == '__main__': main()
    

I'm trying to make a simple text based game where you move the '@' around a
grid of '#'s and try to find the exit. I've changed the code to make it easier
for me to make the grid bigger or smaller without adding or deleting huge
chunks of code and it keeps on giving me this output:

    
    
    fail
    # # # # #
    @ # # # #
    @ # # # #
    @ # # # #
    @ # # # #
    11 52
    

and I can't figure out what's wrong with it! Only one '@' is supposed to
appear :( I am only a newbie at python so if you have any tips for improving
this please, don't hesitate, and post them! Thanks in advance,


Answer: I think the "fail" occurs because it will occur every time the char_loc is not
between 51 and 55.

    
    
    if (char_loc >= 11 and char_loc <= 15):
    if (char_loc >= 21 and char_loc <= 25):
    if (char_loc >= 31 and char_loc <= 35):
    if (char_loc >= 41 and char_loc <= 45):
    if (char_loc >= 51 and char_loc <= 55):
    else:
    

What I think you'd want to do here is use elif, which will only fire if the
previous checks don't trigger.

    
    
    if (char_loc >= 11 and char_loc <= 15):
    elif (char_loc >= 21 and char_loc <= 25):
    elif (char_loc >= 31 and char_loc <= 35):
    elif (char_loc >= 41 and char_loc <= 45):
    elif (char_loc >= 51 and char_loc <= 55):
    else:
    

In regards to the multiple @ symbols, I think this may play a part. Currently
you have:

    
    
    if char_loc == k:
        x0.insert(i, '@')
    else:
        i += 1
        k += 1
    

What I think you're looking to do is:

    
    
    if char_loc == k:
        x0.insert(i, '@')
    
    i += 1
    k += 1
    

Since you want k to change every time that loop iterates.

One last thing that I would suggest is since you have:

    
    
    i =0; k = 21
    i =0; k = 31
    i =0; k = 41
    i =0; k = 51
    

You will probably want to add

    
    
    i =0; k = 11
    

To the first one.

Hope that helps.

