Page 1 of 1

return this code with only one Value def flight(height): """ recursively calculates the path of a hailstone :param heigh

Posted: Sat May 14, 2022 4:35 pm
by answerhappygod
return this code with only one Value
def flight(height):
"""
recursively calculates the path of a
hailstone
:param height: the height of the
hailstone
:return: a recursive call, which at the end
returns the number
of "steps"taken for the
hailstone to reach a height of 1
"""
#### BASE CASES:
# if height is zero or lower, print out an
"invalid" message and return 0
if height<=0:
print("Invalid height
of",height)
return 0
# stops when it reachs height of 1 (the
ground)
if height == 1:
print("Height of",height)
return (flight,), 1
#### RECURSIVE CASES:
# if the current height is even, divide it by
2
if height % 2 == 0:
print("Height of",height)
rest =
flight(height//2)
#odd
else:
print("Height of",height)
rest =
flight(height*3+1)
return (height,) + rest[0], rest[1] +
1
if __name__ == "__main__":
print("Welcome to the Hailstone
Simulator!")
msg = "Please enter a height for
the hailstone to start at: "
start_height = int(input(msg))
steps=flight(start_height)
if steps!=0:
steps=steps[-1]-1
# recursive call goes here
print("\nIt took", steps, "steps to hit
the ground.")
print("Thank you for using the Hailstone
Simulator!\n")