Write a function juggle that has one parameter num The funct

Write a function juggle() that has one parameter: num.

The function generates a sequence of numbers as follows:

It starts with num

If num is even, it replaces num with the integer value of num **0.5

If num is odd, it replaces num with the integer value of num **1.5

This repeats until it hits num == 1, printing all the values of num along the way

The function prints the numbers, each one on a line by itself, including the starting value of num and the ending value of 1. This time do it with recursion. DO NOT use any type of loop (while or for). Make sure to include a meaningful docstring. I included in documents for this assignment, a file named halves_recursive.py. Your solution will be similar except you will have two recursive steps.

Test calling function with num = 8, 17

Solution

def juggle(n):
    if n<=1:
        return n
    else:
        if n%2==0:
            n=int(n**0.5)
            print n
            juggle(n)
        else:
            n=int(n**1.5)
            print n
            juggle(n)
num= input(\"Enter a number:\")
print num
juggle(num)

Output:

Test Case1:

Enter a number: 8
8
2
1

Test Case2:

Enter a number:17
17
70
8
2
1

Write a function juggle() that has one parameter: num. The function generates a sequence of numbers as follows: It starts with num If num is even, it replaces n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site