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...
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...
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.
With reference to Exercise 11.5 on page 398 (a) evaluate s^2; (b) construct a 95% confidence...
With reference to Exercise 11.5 on page 398 (a) evaluate s^2; (b) construct a 95% confidence interval for β0 (c) construct a 95% confidence interval for β1
Exercise 2-40 Prepare Statements for a Manufacturing Company (LO 2-2, 4) The following balances are from...
Exercise 2-40 Prepare Statements for a Manufacturing Company (LO 2-2, 4) The following balances are from the accounts of Crabtree Machining Company: January 1 (Beginning) December 31 (Ending) Direct materials inventory $ 97,300 $ 109,400 Work-in-process inventory 108,400 107,200 Finished goods inventory 43,000 46,300 Direct materials purchased during the year amount to $513,200, and the cost of goods sold for the year was $1,875,000. Required: Prepare a cost of goods sold statement.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT