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.