-1

I just started learning python last week and I have a homework assignment that wants me to design code that outputs the correct answer to this problem:

A cyclist peddling on a level road increases from 3 mph to 15 mph in 0.5 hours.

The equation below can be used to determine the rate of acceleration, where A is acceleration, t is time interval in hours, iV is initial velocity, and fV is final velocity.

A = (fV – iV)/t

Determine the rate of acceleration for the cyclist (in miles/hr^2) assuming that the cyclist continues to accelerate a constant rate for the first 0.5 hours.

Now at first, I had no idea how to go at this problem, but then I put in values in the equation and got A = (15 - 3)/0.5 = 24

I'm not sure what the units to 24 are, I think they're miles per minute or something. I know I have some unit conversion issue, but for now this slightly incorrect math interpretation is what i'm working with.

My code is as follows:

def main():
    InitialVelocity = 15
    FinalVelocity = 3
    TimeIntervalInHours = 0.5
    RateofAcceleration = (FinalVelocity-InitialVelocity)/TimeIntervalInHours
    print(RateofAcceleration)

This prints nothing.

Another code I developed is as follows:

InitialVelocity = 15
FinalVelocity = 3
TimeIntervalInHours = 0.5
NewVelocity = FinalVelocity - InitialVelocity
RateofAcceleration = NewVelocity/TimeIntervalInHours
print(RateofAcceleration)

Error message:

SyntaxError: multiple statements found while compiling a single statement

What can I do to change my code to correctly model this math problem and output the answer?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Welcome to StackOverflow! How are you calling running this code? If your "main()" function is in a file that you're executing (e.g. by calling `python myfile.py`, you'll need to actually call the function in the script (i.e. calling `main()`) after the definition. P.S. I didn't downvote you but you probably got the down vote because SO is not a general homework help platform, and this type of thing could probably be answered by reading or watching a python tutorial... – Jthorpe Jan 27 '20 at 02:04
  • Variable and function names should follow the `lower_case_with_underscores` style. – AMC Jan 27 '20 at 02:25
  • I can't reproduce the SyntaxError. Output is `-24.0`. You need to provide a [mre]. BTW welcome to Stack Overflow! Check out the [tour] and [ask]. – wjandrea Jan 27 '20 at 03:37
  • Oh wait, I googled the error and found this: [SyntaxError: multiple statements found while compiling a single statement](https://stackoverflow.com/q/21226808/4518341). The error seems to come from a limitation of IDLE (and maybe some other Python REPLs?) when copy-pasting code in. I was running your code as a script. – wjandrea Jan 27 '20 at 03:40

1 Answers1

1

Program 1 Failure: Your program is in main method, which is never called. Try calling main() initially after the function in the global scope and it should do something. Also, your velocity variables are flipped.

Program 2 Failure: No idea. Runs fine on mine besides the wrong ordering of velocities in the equation. It might have something to do with another component of your program. Are you compiling in Python 3 (python -V in terminal, or python3 equivalent if you're on a Mac without many special configurations)?


Since you're new, the following is how I might implement this program to be more readable. (Though I have a different naming convention and would probably just do it in fewer lines for brevity).

Converting to variables: A cyclist peddling on a level road increases from 3 miles/hr to a speed of 15 miles/hr in 0.5 minutes:

iV = 3      # Initial velocity, mph
fV = 15     # Final velocity, mph, after 0.5 minutes
t  = 0.5    # Time, hours

Implementing Equation: A = (fV – iV)/t. For unit operations (mi/h - mi/h)/h is m/h^2, as is given.

A = (fV – iV)/t     # Implementing formula
print(A)            # 24, as you guessed

Longer way to approach equation:

dV = (fV - iV)      # Delta V (change in velocity)
A  = dV/t

Notice that by using shorter variable names and uniform spacing, the code is so much easier to read. Try working on it. Also, adapt some kind of coding style while you're at it. Personally, I would have used iv, fv, t, dv, and a for single-value elements like these for good style. Just Google good Python coding style, naming conventions, etc.

Also, Welcome to StackOverflow! and avoid asking homework questions as a general rule of thumb: they're trivial to find the answer to online.

Vadim
  • 642
  • 1
  • 4
  • 19
  • I answered because you already were very close. Just make sure to internalize the advice for next time. Good luck! – Vadim Jan 27 '20 at 02:46