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

  • Thread starter BiGyElLoWhAt
  • Start date
  • Tags
    Python
In summary: I changed the code to use distance = distance +... etc., it stopped the endless loop. However, if I remove the += and just use distance = distance, the code keeps running and never finishes. This might be because I'm not giving it the correct number of milliseconds, or because my code is trying to add a number to itself (which is not allowed).
  • #1
BiGyElLoWhAt
Gold Member
1,622
131

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: 386
Technology news on Phys.org
  • #2
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:
  • #3
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.
 
  • #4
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
  • #5
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.
 
  • #6
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
  • #7
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.
 
  • #8
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
  • #9
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.
 

1. How do I get started with learning Python?

To get started with learning Python, it is important to first understand the basics of programming, such as variables, data types, and control structures. Once you have a good understanding of these concepts, you can start learning Python syntax and its built-in functions. There are many online resources and tutorials available to help you get started.

2. How can I improve my Python coding skills?

One of the best ways to improve your Python coding skills is to practice regularly by working on coding challenges and projects. You can also read and study code written by others, as well as attend coding workshops or take online courses. It is also important to regularly review and refactor your own code to improve its efficiency and readability.

3. What are some common mistakes to avoid in Python programming?

Some common mistakes to avoid in Python programming include using incorrect indentation, forgetting to close parentheses or quotation marks, and using the wrong data types. It is also important to avoid using global variables and to properly handle errors and exceptions in your code.

4. How can I debug my Python code?

To debug your Python code, you can use tools such as print statements, debugging libraries, or integrated development environments (IDEs). You can also use the "pdb" module to step through your code line by line and identify any errors or bugs.

5. What are the benefits of using Python for scientific computing?

Python is a popular language for scientific computing because it has a large and active community, a wide range of libraries and tools for data analysis and visualization, and a simple and readable syntax. It also allows for easy integration with other languages and platforms, making it a versatile choice for scientific projects.

Similar threads

  • Programming and Computer Science
Replies
5
Views
960
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
305
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
16K
  • Programming and Computer Science
Replies
8
Views
870
Back
Top