Concerns about Python Scoping

Asked 2 years ago, Updated 2 years ago, 14 views

nx,ny = 0,0 #startcoordinates

for i in range(n):
    result = 0
    for _ in range(7):
        ax = int(random.randrange(1, 16))
        ay = int(random.randrange(1, 16))
        distance = math.sqrt(abs(ax - nx) **2 + abs(ay - ny) **2)
        Cumulative distance at result += distance #result
        nx = ax #Put in the variable because the next distance must be the previous distance
        ny = ay
    distance = math.sqrt(abs(ax) **2 + abs(ay) **2) #sum the distance returned from the last coordinate to the origin
    Result += distance # Cumulative distance in result

    arr1.append(result)
    a = np.array([arr1])
    AVG = np.mean(a) #Mean the simulation

for i in range(n):
    result = 0
    for _ in range(2):
        bx = int(random.randrange(16, 41))
        by = int(random.randrange(16, 41))
        distance = math.sqrt(abs(bx - nx) **2 + abs(by - ny) **2)
        Cumulative distance at result += distance #result
        nx = bx #Put in the variable because the next distance must be the previous distance
        ny = by
    distance = math.sqrt(abs(bx) **2 + abs(by) **2) #sum the distance returned from the last coordinate to the origin
    Result += distance # Cumulative distance in result

    arr2.append(result)
    a = np.array([arr2])
    AVG = np.mean(a) #Mean the simulation

for i in range(n):
    result = 0
    for _ in range(1):
        cx = int(random.randrange(41, 101))
        cy = int(random.randrange(41, 101))
        distance = math.sqrt(abs(cx - nx) **2 + abs(cy - ny) **2)
        Cumulative distance at result += distance #result
        nx = cx #Put in the variable because the next distance must be the previous distance
        ny = cy
    distance = math.sqrt(abs(cx) **2 + abs(cy) **2) #sum the distance returned from the last coordinate to the origin
    Result += distance # Cumulative distance in result

    arr3.append(result)
    a = np.array([arr3])
    AVG = np.mean(a) #Mean the simulation

Hi, how are you?

We're going to randomly pick the x and y coordinates to find the distance between them I was wrong

As shown in this figure, 7

on the (15-15) coordinates

Two on (40~40) coordinates excluding (15~15)

We tried to derive one from (100-100) coordinates excluding (40~40) but

It was a stupid way to derive this...

Can you find an expression to draw the coordinates shown in the picture above?

python

2022-09-20 14:39

2 Answers

 As shown in the figure (15 to 15) 7 on coordinates
2 on [*1] (40~40) coordinates excluding (15~15)
[*2] (100-100) coordinates excluding (40~40) were trying to derive one of these

I think you can just code the words above.

import random
ax,ay = 0, 0
while(True): #16x16
    ax = int(random.randrange(1, 16))
    ay = int(random.randrange(1, 16))
    break

while(True): #40x40
    ax = int(random.randrange(1, 40))
    ay = int(random.randrange(1, 40))
    if not (ax < 16 and ay < 16): break # [*1]
    #Exit if not in 16x16 rectangle

while(True): #100,100
    ax = int(random.randrange(1, 100))
    ay = int(random.randrange(1, 100))
    if not ( ax < 40 and ay < 40 ): break # [*2]
    #Exit if not in 40x40 rectangle


2022-09-20 14:39

Collecting all three from one roof.


import random

A, B, C = [], [], [] # A will store small square areas, middle areas in B, and random coordinates in C.

while True:
    x, y = random.randrange(1, 101), random.randrange(1, 101)
    # # print(x, y)
    If (x <=15) and (y <=15): # The additional field is the square area
        if len(A) < 15:
            A.append((x, y))
    elif (x <=40) and (y <=40): # Intermediate area
        if len(B) < 2:
            B.append((x, y))
    else: # Rest of Area
        if len(C) < 1:
            C.append((x, y))
    If (len(A) == 15) and (len(B) == 2) and (len(C) == 1): # If A, B, and C are gathered as much as you want, exit the loop.
        break

for x, y in A:
     # # do your math for A
for x, y in B:    
     # # do your math for B
for x, y in C:    
     # # do your math for C

I'll add an answer.

You can pick randomly first, and since you have gathered them, you can pick one for each area and continue to do what you want. OK?


2022-09-20 14:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.