Python Forum
Changing for loop to while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing for loop to while loop
#1
Can someone help me out in changing this for loop to a while loop
hits=0.0
arrows=100
For i in range(arrows):
    Randx=random.uniform(-1,1)
    Randy=random.uniform(-1,1)
    if (randx**2+randy**2)<1:
        hits+=1.0
Reply
#2
What have you tried? Post your attempt in Python code tags.
In case you are completely stuck, you will probably want to use a variable as a counter (for 100 arrows) and increment/decrement it in each iteration. And of course check for counter's value in the while loop condition.
Reply
#3
The following link may help you with the while loop: http://www.pythonforbeginners.com/loops/...ile-loops/

Please note that Python is CASE SENSITIVE.

In your existing code, For should be for. According to the Python style guide (PEP8) randx and randy should be lower case. See https://www.python.org/dev/peps/pep-0008/ See the 'function and variable names section': https://www.python.org/dev/peps/pep-0008...able-names

You should probably add a debugging print statement to see if your code is working correctly. Maybe something like:
print(randx, randy, randx**2+randy**2, hits)
When testing code with random numbers, it is sometimes difficult to debug the code because each time you run the code, the random number sequence is different. For testing purposes you can add the following line at the beginning of your code (after the import statement) to generate the same random number sequence each time. The number inside the parentheses is the 'seed number' which you can change to any number you like. When you are done debugging, you can remove the line that seeds the random number generator.
random.seed(12345)
I hope this helps.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#4
General approach for such transforming is the following:

for item in some_iterable:
    # any python code here

# can be rewritten as:

while True:
    try:
        item = next(some_iterable)
    except StopIteration:
        break
    # any python code here
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 425 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  for loop not executing Abendrot47 2 227 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 625 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 822 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 498 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 377 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 420 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 450 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 632 Dec-28-2023, 05:14 PM
Last Post: trix
  Need an alternative to brute force optimization loop jmbonni 5 1,191 Dec-07-2023, 12:28 PM
Last Post: RockBlok

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020