Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

ISBN: 3319234609, 978-3319234601

More Books

Students also viewed these Databases questions

Question

What is the LOOP, and how is it different from APPP?

Answered: 1 week ago