Why is the duration an integer and why am I not getting any output?

  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion revolves around a Python script designed to simulate the distance a rocket travels in the first two seconds of flight, based on random firing durations of 1 to 100 milliseconds. The user initially faced issues with the duration variable not changing, leading to an infinite loop. After troubleshooting, it was discovered that the duration was not being updated correctly due to the random number generation process. The user modified the code to ensure that the duration was calculated correctly and adjusted the loop conditions to avoid unnecessary parentheses. They also clarified that the `+=` operator is valid in Python, which resolved some confusion regarding variable updates. The final output showed a reasonable distance traveled, indicating that the code was functioning correctly after the adjustments. The conversation highlighted the importance of debugging techniques, such as using print statements to track variable changes and understanding Python's syntax for loops and conditionals.
BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138

Homework Statement



A rocket can either fire or not fire for a duration of 1-100ms with equal probability of firing/not firing. Determine how far it will go in the first 2 seconds of flight.

Homework Equations

The Attempt at a Solution


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import random
fire = 0
duration = 0.0
distance = .0
velocity = .0
time = 0.0
while (time < 2.0):
...duration = (random.randint(1,100))/1000.0
...if (time + duration > 2.0):
...duration = 2.0 - time

...fire = random.randint(0,1)
...time += duration
...distance += (1/2)*2*fire*(duration**2) + velocity*duration
...velocity += 2*fire*duration

print("distance: " , distance)

4. Questions about this:
So one of my first questions is why is duration an integer? I've tried several things, putting decimal places in differently etc, to make it a float, but I can't seem to get it. I'm assuming it is changing types with the randint() function, but what should I use otherwise? Also, shouldn't dividing by 1000.0 change it to a float?
Also, I'm getting litterally nothing as an output, which makes me think it's getting caught in this loop here. I don't python much, or really code a whole lot anymore, so I'm probably pretty rusty and making noob mistakes. Is there something obvious in here that's not letting it leave the loop? I'll walk through my logic, in case it isn't obvious.

Initialize variables
while time elapsed is less than 2 seconds, do stuff:
stuff{
get a duration using rand numbers
if time elapsed plus duration is greater than 2 seconds, set duration = to the difference, so that time elapsed is exactly 2 seconds
determine whether the rocket fires over the duration or not.
Calculate the distance traveled using the initial velocity at the beginning of the fire/not fire phase, and the acceleration from that fire/not fire phase (the acceleration function was given to us)

calculate the new velocity to be used for the next calculation
repeat
spit out distance traveled.
end.

Worth noting:
mass lost from fuel burning is negligible
air resistance is negligible.
the variable 'a' in the console in the attached image is just a variable that I used to make sure that I could print like I thought I could. It worked. It's irrelevant to the rockets code.
 

Attachments

  • rocket.jpg
    rocket.jpg
    21.3 KB · Views: 434
Technology news on Phys.org
Update:
It has to be my random. Literally none of my variables are changing as I step through this. I've tried 2 different random's at this point, I've tried randint(low,high) and randrange(low,high,step).
debugfile('C:/Users/Tyler/.spyder2/rocket - tyler witte.py', wdir='C:/Users/Tyler/.spyder2')
continue
continue
continue
continue
continue
continue
continue
continue
continue
continue
...x~50 more (removed for the absurd amount of continues)
continue

Even my duration isn't changing. I don't know what's up with this. Is there a better way to get random numbers? I know in java I used to use something to the effect of gettimemilliseconds(), and that would work perfect, but this isn't spitting out random numbers (unless literally about 200 in a row happened to be zero, which I find unlikely), and so it's getting stuck in the loop indefinitely.

Edit*
OK, I think I fibbed a little. I had to refresh my variable explorer each time. Fire is changing back and forth between 0 and 1, but duration is not at all.
 
Last edited:
Disregard. I noticed I wasn't able to get it to stop running, so I closed out the console and reopened it, and then it worked, but I changed all the += to distance = distance +... etc. and also ditched all the floats. So changed time <2.0 to time < 2000 and then just divided my distance by 1000.0 at the end. I'm getting a float result for this, and it seems somewhat reasonable, between 1500 and 2500 meters each time.
*Edit
I went through and changed most everything back, and it's working as well the other way. Perhaps += isn't a thing in python, I thought it was, however. Could someone clarify this?
I got to thinking, and with the duration**2 term, it wouldn't work right, that is to give the proper distance. I'm now getting between 1 and 3 meters each time, which seems more feasible.
 
BiGyElLoWhAt said:
Disregard. I noticed I wasn't able to get it to stop running, so I closed out the console and reopened it, and then it worked, but I changed all the += to distance = distance +... etc. and also ditched all the floats. So changed time <2.0 to time < 2000 and then just divided my distance by 1000.0 at the end. I'm getting a float result for this, and it seems somewhat reasonable, between 1500 and 2500 meters each time.
*Edit
I went through and changed most everything back, and it's working as well the other way. Perhaps += isn't a thing in python, I thought it was, however. Could someone clarify this?
I got to thinking, and with the duration**2 term, it wouldn't work right, that is to give the proper distance. I'm now getting between 1 and 3 meters each time, which seems more feasible.

+= and all of the other compound assignment operators are valid operators in Python. I noticed one line that might be causing problems for you
Python:
...if (time + duration > 2.0):
Python syntax doesn't use parentheses around the Boolean expression (unlike C and derived languages). Same for a while loop.

I haven't tested your code to see if the items I mentioned are problematic, so having the extra parentheses might not be the problem. Your duration variable should be integral, though, I believe, as the granularity of duration is supposed to be in (whole) milliseconds.

Some other things I noticed that may or may not be problems are that you initialized duration and velocity to .0, which at the very least looks odd to me.
BiGyElLoWhAt said:
Edit*
OK, I think I fibbed a little. I had to refresh my variable explorer each time. Fire is changing back and forth between 0 and 1, but duration is not at all.
If time is not less than 2.0, the body of the while loop won't be executed.
 
  • Like
Likes BiGyElLoWhAt
Well,the parentheses turned out to not be a problem, as I got it to run properly. If += is a valid operator, then I don't know why it didn't work earlier. My thinking was that it wasn't compounding, and thus time was never increasing, so the loop executed continuously, without end.

I want it to stop when time = 2, as I'm looking for the distance traveled in the first 2 seconds.

I suppose I could have initialized to 0.0, I understand it looks odd, but wouldn't it have given me a syntax error if that wasn't correct? Maybe that had something to do with it. I'm not sure.
 
Put a breakpoint inside your while loop, and see if the breakpoint ever gets hit. I suspect that the condition for your while loop is always false.

I wrote an Insights tutorial on how to use the built-in Python debugger (Pdb), if you're not sure how to do this.
 
  • Like
Likes BiGyElLoWhAt
I did do that earlier, I actually put several. That's what all the "continue"s are up above. It got hit every time, so I think that for whatever reason, time wasn't increasing. When I would refresh my variable explorer or whatever it's called, duration was always equal to zero, while fire flipped back and forth between 0 and 1 like it should. I don't know why, though.
 
Here's your code with a few minor changes:
  • Removed extraneous parenthese on while header and if header
  • Removed (1/2) * 2 in calculation of distance
  • Change initializations of distance and velocity from .0 to 0.0
  • Added two print() calls to print value of duration - one of them before the if statement, and the other in the body of the if statement
Python:
import random
fire = 0
duration = 0.0
distance = 0.0
velocity = 0.0
time = 0.0
while time < 2.0:
   duration = (random.randint(1,100))/1000.0
   print("(1) duration: ", duration)
   if time + duration > 2.0:
      duration = 2.0 - time
      print("(2) duration: ", duration)
   fire = random.randint(0,1)
   time += duration
   distance += fire*(duration**2) + velocity*duration
   velocity += 2*fire*duration

print("distance: " , distance)
Output of the code above:
Code:
(1) duration:  0.003
(1) duration:  0.068
(1) duration:  0.023
(1) duration:  0.06
(1) duration:  0.094
(1) duration:  0.056
(1) duration:  0.073
(1) duration:  0.025
(1) duration:  0.066
(1) duration:  0.016
(1) duration:  0.056
(1) duration:  0.043
(1) duration:  0.053
(1) duration:  0.001
(1) duration:  0.005
(1) duration:  0.072
(1) duration:  0.071
(1) duration:  0.064
(1) duration:  0.092
(1) duration:  0.052
(1) duration:  0.041
(1) duration:  0.096
(1) duration:  0.039
(1) duration:  0.022
(1) duration:  0.091
(1) duration:  0.042
(1) duration:  0.001
(1) duration:  0.061
(1) duration:  0.039
(1) duration:  0.037
(1) duration:  0.041
(1) duration:  0.091
(1) duration:  0.063
(1) duration:  0.073
(1) duration:  0.027
(1) duration:  0.018
(1) duration:  0.022
(1) duration:  0.086
(1) duration:  0.026
(1) duration:  0.035
(1) duration:  0.075
(2) duration:  0.056000000000000494
distance:  2.4975050000000008

I don't understand what your code is supposed to do, so I don't know how to correct it. The output above indicates that the condition for your if statement isn't satisfied for about 30 or so iterations of the while loop. From this I can infer that time + duration <= 2.0 until the next to last line of output above.
 
  • Like
Likes BiGyElLoWhAt
That looks comparable to my output, minus all the duration prints. This code is only supposed to run until time= 2 seconds, and then it should terminate and print the distance traveled in that time.

I left the 1/2*2 in there because the acceleration was 2m/s^2 and the 1/2 is from integration. Just to make it a little more obvious to my prof that that's where that came from.

Like I said earlier, I don't know why, but my duration was sitting at zero, and if that doesn't change, then time can't increase. I don't know if there was a hiccup in my code somewhere causing this or what, but it's fixed now.

Thanks for answering my questions.
 
Back
Top