Question

In: Computer Science

Exercise 2. Automate the boring stuff, page 77, Input Validation. Add try and except statements to...

Exercise 2. Automate the boring stuff, page 77, 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.

THis is the previous project

#function definition
def collatzSequence(number):
#
    if number % 2 == 0:
        print((number) // 2)
        return number // 2

    else:
        nextNumber = 3*(number)+1
        print(nextNumber)
        return nextNumber

n = input("Please input an integer: ")
while n != 1:
    n = collatzSequence(int(n))

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#function definition
def collatzSequence(number):
#
if number % 2 == 0:
print((number) // 2)
return number // 2

else:
nextNumber = 3*(number)+1
print(nextNumber)
return nextNumber

while True:
n = input("Please input an integer: ")
try:
n=int(n)
break
except:
print('INVALID.. Please Enter an Integer.')

  
while n != 1:
n = collatzSequence(int(n))

==========================

SCREENSHOT:


Related Solutions

Using Python In this assignment we will try to add tuples, lists, if statements and strings...
Using Python In this assignment we will try to add tuples, lists, if statements and strings to our program. For example, you can ask for a user for a couple items, their name and age – and depending on their age, print out a predefined list. You could ask for some string input and decide to do something with the output. You could ask for three items, 1) age, 2) are you a male or female and 3) are your...
For practice using exceptions, this exercise will use a try/catch block to validate integer input from...
For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...
Summary In this lab, you add the input and output statements to a partially completed Java...
Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write the simulated housekeeping() method...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
Writing a Modular Program in Python In this lab, you add the input and output statements...
Writing a Modular Program in Python In this lab, you add the input and output statements to a partially completed Python program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared...
Text exercise 39 page 638. This question uses the same data as exercise 2 above, and...
Text exercise 39 page 638. This question uses the same data as exercise 2 above, and the data is in the accompanying spreadsheet. (a) Estimate the regression in Excel and report the regression line.                                  [2 pts] (b) Calculate a  95% confidence interval for the forecast of the average amount spent on entertainment at a city where the room rate is $89.                                                       [3 pts] (b) Calculate a  90% confidence interval for the forecast of the idiosyncratic amount spent on entertainment at a city where the...
In a study published in Medicine and Science in Sports and Exercise (volume 17, no.2, page...
In a study published in Medicine and Science in Sports and Exercise (volume 17, no.2, page 189) the measurements of 252 men (ages 22 – 81) are given. Of the 14 measurements taken of each man, some have significant correlations and others don’t. The partial table shown below lists only the first 9 rows of data and 9 of the measurements. Age (yr.) Weight (lb.) Height (in.) Neck (cm) Chest (cm) Wrist (cm) Hip (cm) Thigh (cm) Knee (cm) Body...
Add a 1- to 2-page section to your Playbook/Runbook. Create a prescriptive section of your playbook...
Add a 1- to 2-page section to your Playbook/Runbook. Create a prescriptive section of your playbook providing guidelines to secure each of these areas: Network connections Mobile devices Cloud services
QUESTION 2 All the following statements are true about corporate reorganization except: a. Taxable amounts for...
QUESTION 2 All the following statements are true about corporate reorganization except: a. Taxable amounts for shareholders are classified as a dividend or capital gain. b. Reorganizations receive treatment similar to corporate formations under § 351. c. The transfers of stock to and from shareholders qualify for like-kind exchange treatment. d. The value of the stock received by the shareholder less the gain not recognized (postponed) will equal the shareholder’s basis in the stock received. e. All of these are...
Create a page with jQuery. 1. Add "IMG.jpg" with size WIDTH="200" HEIGHT="60", give an id. 2....
Create a page with jQuery. 1. Add "IMG.jpg" with size WIDTH="200" HEIGHT="60", give an id. 2. Use mouseenter(), width(), and height() to resize the picture to 800 by 230. 3. Use mouseleave, width(), and height() to resize the picture to 200 by 60.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT