简体   繁体   中英

Nested If Loop in For loop does not work (Python)

I have the following loop which does not seem to work and I can't figure out why. Here are my variables first:

Joe_speed = 1.25  ##speed in m/s --> 4.5km/h = ((4.5*1000)/60)/60) = 1.25m/s
Jill_speed = 1.9
dog_speed = 5
total_distance = 5500 ## distance in meter between Joe and Jille when starting

JJtotal_speed = Joe_speed + Jill_speed
total_time = total_distance / JJtotal_speed ##time in seconds until meeting 
totaltime = int(total_time)
Jill_distance = total_time * Jill_speed
Joe_distance = total_time * Joe_speed
dog_start = total_distance


for i in range (1, (totaltime +1)*1000) : # multiplied by 1000 for milliseconds
    Jill_position = 0 + ((Jill_speed/1000) * i)
    Joe_position = total_distance - ((Joe_speed/1000) * i)
    dog_position = dog_start - ((dog_speed/1000) * i)
    if dog_position == Jill_position or dog_position == Joe_position:
        print("Hi")
    else:
        print("Ciao")   

My if loop within the for loop does not seem to work however. Somehow neither "Hi" nor "Ciao" is being printed, ie the if loop is not happening? When I add print("...") at the end of the for loop (on the very left side on the bottom, this is being printed.

Can someone help me with this? Thanks!

Most likely, the value of totaltime is such that the for loop is never executed. You can check by adding a print statement as the first statement in the for loop.

[Update]

Based on your updated code, the answer above is correct. Here's what's happening:

  1. totaltime evaluates to 1746.
  2. Your range upper limit is totaltime / 1000 . When dividing two integers in Python, the result is integer , created by truncating the resulting floating point number. So 1746 / 1000 is 1 in Python.
  3. Your range is therefore 1 to 1 and the for loop is never executed.

You can avoid the integer truncation issue by making one of the numbers in the expression a floating point number (that is, writing 1000.0 instead of 1000 , but that doesn't help because range needs integer arguments.

Your first problem is that, if totaltime is 1746, then (totaltime +1) //1000) is 1. So you're looping over range(1, 1) , which means you're not looping at all.

Your second problem is that the scales are all way off, probably because you got one * and / mixed up somewhere. Everyone is moving so slowly that it'll take a ridiculous number of steps before Joe meets Jill.

But even if you ignore your totaltime and add a huge number of steps, neither of them ever meet the dog. They're not moving continuously, they're moving discretely. So Joe ends up crossing the dog's position between two steps.

Try adding a print call to see all three positions at each step, and you'll see all of these problems.

Even if you tweaked the numbers so that one of the people should meet the dog exactly at one of the steps, it might still fail. You're using floats. Most real numbers can't be represented exactly as floats, so the result of two different calculations that should both come out to 900.0 in ideal math could come out to just over 900 and just under 900 with float math. You could solve this by using math.isclose instead of == . But you need to solve the previous problem anyway, and doing that will solve this one.

So, after you solve the calculation for the first two problems, how can you solve the last two? You need to use <= and >= in appropriate ways instead of == . Which probably means keeping track of what the results were last time through the loop. If Joe < dog now, but Joe > dog a step ago, they've crossed each other. That's what you need to test for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM