0

I'm currently trying to create a code that calculates how long a spaceship will take to travel a certain distance, every minute it will jump half the remaining distance. If there is a distance of less than 1 meter left, it will only take one more minute.

def space_time(d,t=0):

    if d <= 1:
        print("- It takes 1 minute to travel", d, "meters")
    elif d > 1:
        t = t + 1
        return space_time(d / 2, t)
    else:
        t = t + 1
        print("- ", t, "minutes to travel", d, "meters")


(space_time(10))

Output:

- It takes 1 minute to travel 0.625 meters

Process finished with exit code 0

I can see that my problem is the t = t + 1. My idea for this was every time the function is repeated it would add 1 to t which would signify 1 minute. But currently its not working. Any help would be greatly appreciated.

3
  • 1
    Mathematically, it'll never travel the entire distance Commented Mar 14, 2017 at 19:59
  • 1
    Please indicate why your code is not working by explanation or by showing the output. Also, you need to reevaluate the logic in your code. For instance, the comparisons in your if and elif statements are both d<=1. Commented Mar 14, 2017 at 20:04
  • Until the distance is less than 1 than it takes only another minute. Commented Mar 14, 2017 at 20:08

2 Answers 2

1

A problem is that it is impossible for the "elif d <= 1:" statement to get called, because the condition is exactly the same as the "if" condition above, which means this code will never be runned. You probably should change the "elif" to "if" if you want them both to be executed, or even better, merge them together

    if d <= 1:
    print("- It takes 1 minute to travel" , d, "meters")

    elif d <= 1:
    t = t + 1
    return space_time(d/2)
Sign up to request clarification or add additional context in comments.

Comments

1

There's a couple of problems here. For one, your 'elif' statement is checking the same condition as your if! I think you want 'greater than'.

Second, you are always setting t to 0 at the beginning, and not passing that back with the recursive loop. The original d is also not passed back.

Finally, I'm not sure what the else statement is supposed to be catching.

EDIT: If 0 < d < 1, then it takes one more minute. The last else captures this now.

def space_time(d_0,d=None,t=0):
    if d is None:
        d = d_0
    if d <= 0:
        print("- It takes", t, "minutes to travel" , d_0, "meters")

    elif d > 1:
        t = t + 1
        return space_time(d_0,d/2,t)

    else:
        print("- It takes", t + 1, "minutes to travel" , d_0, "meters")

(space_time(10))

Output:

- It takes 5 minutes to travel 10 meters

4 Comments

Hey thanks for this, you were correct about my if and elif doing the same thing. however this is incorrect as whenever the distance is less than 1 meter it takes only another minute.
Okay, I see that now. I've edited the code. I also fixed it so it outputs the proper distance at the end (before it was just printing the last distance). If this helps, make sure to upvote and/or accept the answer!
Hey this was exactly it. Thank you for your help. How do I accept the answer?
Click the check mark next to the answer (I think you had it accepted before and actually unaccepted it...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.