1

Please ignore my un-used import!

I tried to create a list to find min and max of "pa_walk" but I just could figure how to do it, everytime I tried it said error.

import random
from math import sqrt
from math import hypot
import statistics


random.seed(20190101)

def takeOnePaStep():
    direction = random.randint(0,3)
    if direction == 0:
        return (0,1)
    elif direction == 1:
        return (1,0)
    elif direction == 2:
        return (0,-1)
    elif direction == 3:
        return (-1,0)


def randomWalkPa(steps):
    pa = [0,0]
    for _ in range (steps):
        nextStep = takeOnePaStep()
        pa[0] += nextStep[0]
        pa[1] += nextStep[1]
    pasDistance = hypot(pa[0],pa[1])
    return pasDistance

 #   paMean = statistic.mean(distance)

steps = int(input("Please enter the number of steps: "))
tries = int(input("How many times should I perform the experiment? "))

for _ in range(tries):
    pa_walk= randomWalkPa(steps)
    print(pa_walk)
Gwen L.
  • 23
  • 6

1 Answers1

0


I gues it is because your function randomWalkPa(steps) returns a float of the distance, that's why you first need to create a list (in the example below I just made pa_walk a list. In your for-loop just .append the distance for every try to that list. Finally you can call the built-in functions max()and min() to get the maximum and minimum distance. I unindented the print commands for the min and max call to just get the results once

pa_walk = []
for _ in range(tries):
    pa_walk.append(randomWalkPa(steps))

print(f"The Maximum Distance reached was: {max(pa_walk)}, in trial: {pa_walk.index(max(pa_walk))}")
print(f"The Minimum Distance reached was: {min(pa_walk)}, in trial: {pa_walk.index(min(pa_walk))}")

After recommendation in the comments here is the full code (I changed nothing but the last 5 rows)

import random
from math import sqrt
from math import hypot
import statistics


random.seed(20190101)

def takeOnePaStep():
    direction = random.randint(0,3)
    if direction == 0:
        return (0,1)
    elif direction == 1:
        return (1,0)
    elif direction == 2:
        return (0,-1)
    elif direction == 3:
        return (-1,0)


def randomWalkPa(steps):
    pa = [0,0]
    for _ in range (steps):
        nextStep = takeOnePaStep()
        pa[0] += nextStep[0]
        pa[1] += nextStep[1]
    pasDistance = hypot(pa[0],pa[1])
    return pasDistance

 #   paMean = statistic.mean(distance)

steps = int(input("Please enter the number of steps: "))
tries = int(input("How many times should I perform the experiment? "))

pa_walk = []
for _ in range(tries):
    pa_walk.append(randomWalkPa(steps))

print(f"The Maximum Distance reached was: {max(pa_walk)}, in trial: {pa_walk.index(max(pa_walk))}")
print(f"The Minimum Distance reached was: {min(pa_walk)}, in trial: {pa_walk.index(min(pa_walk))}")


Edit:
A minor thing to note, in python it is convention to use underscores rather than camelcase. This means the function randomWalkPa() would be better called random_walk_pa(). This is not necessary to make the code work and totally up to you

Björn
  • 1,610
  • 2
  • 17
  • 37
  • 1
    You might want to put the full code, the one posting the question doesn't seem to be experienced in python – Ardiya Apr 16 '20 at 06:37