Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CPT 180 Chapter 3 Assignment 3 Directions Using the following guidelines, create a python program. 1. Create the Collatz Sequence program as described on page

image text in transcribedimage text in transcribedimage text in transcribed

CPT 180 Chapter 3 Assignment 3 Directions Using the following guidelines, create a python program. 1. Create the Collatz Sequence program as described on page 77 76 in the 2nd Edition). Name the program collatzSeq.py. 2. Add three comment lines at the top of the program that contain: a. Program Name b. Program Description C. Programmer's Name (You) 3. Add code in the program that validates the user's input and ensures it is an integer. If it is not an integer, it displays an error message and re-prompts the user. 4. The collatz() function should always receive one integer as a parameter, perform a calculation, and print out and return an integer. 5. The program should contain a loop that continues to call the collatz() function until the function returns a value of 1. 6. Add code that indicates when the program has ended. 7. See below for an Example Output. 8. Submit the collatzseq.py file into the Chapter 3 Assignment 3 Submission Folder. Hints 1. The error or exception that occurs when the int() function cannot convert a string to an integer is the ValueError exception. Example Output (without and with valid input) La Python 3.8.1 Shell File Edit Shell Debug Options Window Help Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help","copyright", "credits" or "license ()" for more information. - RESTART: G:\TTC_Courses cpt180\CPT180 Version 2\Assignments\Python Solutions collatzSeq2.py Enter an integer to obtain the Collatz Sequence. twenty three Ensure to enter an integer value. Enter an integer to obtain the Collatz Sequence. 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 Program Ended! >>> | Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd. then collatz() should print and return 3 * number + 1. Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer-sooner or later, using this sequence, you'll arrive at l! Even mathematicians aren't sure why. Your program is exploring what's called the Collatz sequence, sometimes called "the simplest impossible math problem.") Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value. Hint: An integer number is even if number % 2 == 0, and it's odd if number % 2 == 1. The output of this program could look something like this: Enter number: 3 10 5 16 8 4 2 1 Input Validation Add try and except statements to the previous project to detect whether the user types in a noninteger string. Normally, the int() function will raise a ValueError error if it is passed a noninteger string, as in int("puppy'). In the except clause, print a message to the user saying they must enter an integer

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

Students also viewed these Databases questions

Question

3. Identify cultural universals in nonverbal communication.

Answered: 1 week ago

Question

Prepare an ID card of the continent Antarctica?

Answered: 1 week ago

Question

What do you understand by Mendeleev's periodic table

Answered: 1 week ago

Question

Know the three main dimensions of the service environment.

Answered: 1 week ago