Question: Create a Python program called moving_to_one that determines the count of times it takes for a number to reach 1 using the following logic. Task

Create a Python program called moving_to_one that determines the count of times it takes for a number to reach 1 using the following logic. Task

A. Create a function called input_value: Accept a number from the user. Validate that number is in the range 100 to 999. Return the number if valid If the number is outside this range, return -1 Task

B: Create a function called reduce_to_one In this function, use a loop and do the following:

If the number is even, reset the number to number/2

If the number is odd, reset the number to (3 * number) + 1

Repeat as long as the number remains greater than 1. Keep a count of the number of iterations until the value of number is 1

Return the count back to main

Note: All positive numbers will reach 1 after a certain number of iterations.

Task C: Create a function called main

Execute the following until the user inputs -1.

Get the user input

pass this number to a function called reduce_to_one. The function should return the count of iterations it took to reach 1

Display the output in the format shown in the example below When the input is -1, terminate the program by displaying "Goodbye!"

Example execution:

Enter a number: 123

It takes 46 iterations for the number 123 to reach 1.

Enter a number: 378 It takes 107 iterations for the number 378 to reach 1.

Enter a number: -1 Invalid number...Goodbye!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!