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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started