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

THIS is to be done in python! Splitting the Bill- An exercise in input validation Write...
THIS is to be done in python! Splitting the Bill- An exercise in input validation Write a python program called splitBill.py that works as follows: • (10 points) Allow the user to input a bill amount, a tip percent, and the number of people in their party. o Input validation: Make sure to validate the input data. Allow the user to reenter inputs until they enter values which are valid. ▪ Make it easy on the user: To leave a...
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...
Find the readData() method Inside this method is a try block without any catch statements. Add...
Find the readData() method Inside this method is a try block without any catch statements. Add the following 5 catch statements to this file (Hint: put them in the right order): RuntimeException Exception FileNotFoundException NumberFormatException InputMismatchException For each of these print out a short message describing the exception that occurred Java Class import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; public class ReadInData {    public static double readData(String fileName) {        File file = new File(fileName);       ...
Exercise 2: Try-Catch Exercise Write a Java code that does the following: Create a class MyClass...
Exercise 2: Try-Catch Exercise Write a Java code that does the following: Create a class MyClass and create three methods myMethod1(), Method2() and Method3(). Invoke Method2() from Method1() and Method3() from Method2(). Write a code that can throw an exception inside myMethod3() and compile: File file=new File("filename.txt"); Scanner sc=new Scanner(file); You will get compilation errors now, as you are not handling a checked exception FileNotFoundException. Declare throws over myMethod3() and myMethod2(). You will need to add throws FileNotFoundException on myMethod()...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT