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
  in c# create a loop counting from 0 to 5, consecutively Frankd 19 2,157 Apr-01-2025, 12:46 PM
Last Post: Frankd
  really new to python want to know how to do a loop pentopdmj 6 1,584 Mar-09-2025, 12:59 PM
Last Post: snippsat
  knowing for loop position in a list medic5678 4 680 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Run this once instead of a loop, do I need the 'calibration' steps? duckredbeard 2 719 Jan-28-2025, 04:55 PM
Last Post: duckredbeard
  Error loop with Chatgpt sportak12 0 494 Jan-14-2025, 12:04 PM
Last Post: sportak12
  How to convert while loop to for loop in my code? tatahuft 4 809 Dec-21-2024, 07:59 AM
Last Post: snippsat
  How to get keep information in a loop ginod 4 882 Dec-11-2024, 01:32 AM
Last Post: deanhystad
  Regarding The For Loop Hudjefa 5 1,455 Nov-15-2024, 01:02 PM
Last Post: deanhystad
  For Loop beginner agoldav 2 715 Nov-05-2024, 12:51 AM
Last Post: agoldav
  A question about 'Event loop is closed' fc5igm 3 5,153 Oct-01-2024, 09:12 AM
Last Post: skdaro

Forum Jump:

User Panel Messages

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