i have this code to simulated a walk going either forwards or backwards amd i need to modify it so that it can go either up, down, left or right. only problem is im stuck can you offer any advice? thanks

import random
filename="walk1.txt"
def randWalk(n):
    steps = 0  #start out at 0
    
    for x in range(n):
       steps += random.choice((-1,1))  #x += 1 means x = x + 1
    return steps  

def main():
    sum=0
    n = input("Enter the number of steps to be taken: ")
    file=open(filename,'w')  
    for x in range  (100):
        sum=0
        result = randWalk(int(n))  #You have to convert str to int
        sum=sum+result
        avg=sum/100
        file.write(str(result))
        file.write(str(avg))
        print("you ended up",result,"steps from the start")
    print("the average distance you went is",avg, "steps from the start")
        
        
    file.close




main()

It's same as you did, but instead of using single digit, you need to use tuples. Tuples could represent the 2 dimensions. (1,1) or (-10, 2). The general form of (x, y).

It's same as you did, but instead of using single digit, you need to use tuples. Tuples could represent the 2 dimensions. (1,1) or (-10, 2). The general form of (x, y).

filename="walk1.dat"
def randWalk(n):
    steps = (0)  #start out at 0
    tup=0
    for x in range(n):
       steps += random.choice((-1,1))  #x += 1 means x = x + 1
    return steps  

def main():
    sum=0
    count=0
    n = input("Enter the number of steps to be taken: ")
    file=open(filename,'w')  
    for x in range  (1000):
        sum=0
        result = randWalk(int(n))  #You have to convert str to int
        sum=sum+result
        tup=(sum,result)
        count=count+1#this sets it up for the following condition, when a 
        if count==1: #certian number of counts is reached it will print both
                     #to the file and the screen
            print(tup),file.write(str(tup))
        if count==10:
            print(tup),file.write(str(tup))
        if count==100:
            print(tup),file.write(str(tup))
        if count==1000:
            print(tup),file.write(str(tup))
                
        
       
        
        
        
    
    distance=sqrt(((0-sum)**2)+(0-result)**2) 
    print("the place you ended is",distance,"steps away fromt the start")   
    file.write(str(distance))
    file.close




main()

you mean something like this? and thanks for the hint

Could you avoid using built in function sum as your variable you end up confusing other and even yourself later with some 'interesting' bugs in your program. If you can not really find other name for the variable, there is standard way to avoid it: use sum_ instead of sum

I do not understand your function, it does not produce 2d movement like on would expect but integer:

import random
from math import sqrt

filename="walk1.dat"
def randWalk(n):
    steps = (0)  #start out at 0
    tup=0
    for x in range(n):
       steps += random.choice((-1,1))  #x += 1 means x = x + 1
    return steps  

def main():
    sum=0
    count=0
    n = input("Enter the number of steps to be taken: ")
    file=open(filename,'w')  
    for x in range  (1000):
        sum=0
        result = randWalk(int(n))  #You have to convert str to int
        print result, type(result) ### ????? Does not look right!
        sum=sum+result
        tup=(sum,result)
        count=count+1#this sets it up for the following condition, when a 
        if count==1: #certian number of counts is reached it will print both
                     #to the file and the screen
            print(tup),file.write(str(tup))
        if count==10:
            print(tup),file.write(str(tup))
        if count==100:
            print(tup),file.write(str(tup))
        if count==1000:
            print(tup),file.write(str(tup))
                
           
    distance=sqrt(((0-sum)**2)+(0-result)**2) 
    print("the place you ended is",distance,"steps away fromt the start")   
    file.write(str(distance))
    file.close


main()

thats my problem i dont know how to make it produce 2d movements thats what im stuck on

Could you avoid using built in function sum as your variable you end up confusing other and even yourself later with some 'interesting' bugs in your program. If you can not really find other name for the variable, there is standard way to avoid it: use sum_ instead of sum

I do not understand your function, it does not produce 2d movement like on would expect but integer:

import random
from math import sqrt

filename="walk1.dat"
def randWalk(n):
    steps = (0)  #start out at 0
    tup=0
    for x in range(n):
       steps += random.choice((-1,1))  #x += 1 means x = x + 1
    return steps  

def main():
    sum=0
    count=0
    n = input("Enter the number of steps to be taken: ")
    file=open(filename,'w')  
    for x in range  (1000):
        sum=0
        result = randWalk(int(n))  #You have to convert str to int
        print result, type(result) ### ????? Does not look right!
        sum=sum+result
        tup=(sum,result)
        count=count+1#this sets it up for the following condition, when a 
        if count==1: #certian number of counts is reached it will print both
                     #to the file and the screen
            print(tup),file.write(str(tup))
        if count==10:
            print(tup),file.write(str(tup))
        if count==100:
            print(tup),file.write(str(tup))
        if count==1000:
            print(tup),file.write(str(tup))
                
           
    distance=sqrt(((0-sum)**2)+(0-result)**2) 
    print("the place you ended is",distance,"steps away fromt the start")   
    file.write(str(distance))
    file.close


main()

i dont know how to make it produce 2d movements

If you have an graph with an x and y axis, you can advance in a postive or negative direction for either axis. To keep it simple, point 1=(3,1) for x and y, point 2=(-3, 2) for x and y, etc., so you have to change both x and y for 2D movements. You appear to be changing x only.

y
            2  |
               |  1
x -------------+-------------------
               |

Instead of having one sum, make it 2; instead of generating one random number, generate 2 and then you have a tuple consisted of 2 elements.

Although you need to use Pythagorean rule to get the distance and alternatively the average distance traveled.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.