Question

Using python, write code for problem. Please also put screenshot of your code.

29. The following program will perform properly if the user enters 0 in response to the request for input. However, the program will crash if the user responds with eight. Rewrite the program using a try/except statement so that it will handle both types of responses. See Fig. 6.1. while True n int (input Enter a nonzero integer if n 0 reciprocal 1 n print (The reciprocal of is (1 ,.3f) format (n, reciprocal)) break else: print (You entered zero Try again Enter a nonzero integer 0 You entered zero Try again Enter a nonzero integer eight You did not enter a nonzero integer Try again Enter a nonzero integer: 8 The reciprocal of 8 is 0.125 FIGURE 6.1 Possible outcome of Exercise 29.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# Stop break #Otherwise else: &Display message print(You entered zero. Try again.) Sample output Python 3.4.3 Shell File Ed

Executable code

#Loop
while True:
   
    #Prompt the user for input
    n = input("Enter a nonzero integer: ")
   
    #Try block
    try:
      
        #Integer
        n = int(n)
   
    #Exception
    except ValueError:
      
        #Print exception
        print("You did not enter a nonzero integer. Try again.")
      
        #Continue to get user input
        continue
   
    #Check condition
    if ( n != 0 ):
       
        #Find reciprocal
        reciprocal = 1/n
       
        #Display reciprocal
        print ("The reciprocal of {0} is {1: .3f}".format (n, reciprocal))
       
        #Stop
        break
   
    #Otherwise
    else:
       
        #Display message
        print("You entered zero. Try again.")

Add a comment
Know the answer?
Add Answer to:
Using python, write code for problem. Please also put screenshot of your code. The following program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

  • Write the pseudocode below as a working Python program. Take a screenshot of your code and...

    Write the pseudocode below as a working Python program. Take a screenshot of your code and output and paste into your answer document. User input of ‘y’ or ‘Y’ should cause the loop to continue.    // Constant for the commission rate    // Declare as a global variable    Constant Real COMMISSION_RATE = 0.10     Module main()        // Local variable         Declare String keepGoing = "y"         // Calculate as many commissions         // as needed.         While...

  • Python homework problem First: Write a program (using a loop with break) to allow a user...

    Python homework problem First: Write a program (using a loop with break) to allow a user to input an integer and determine if the integer is prime. Then change the program to examine a number of integers to determine if each is prime. The program should allow the user to input the number of integers (say n) to be checked to determine if each is prime. Then the user should input n integers and print whether each of the integers...

  • using this code to complete. it python 3.#Display Program Purpose print("The program will show how much...

    using this code to complete. it python 3.#Display Program Purpose print("The program will show how much money you will make working a job that gives a inputted percentage increase each year") #Declaration and Initialization of Variables userName = "" #Variable to hold user's name currentYear = 0 #Variable to hold current year input birthYear = 0 #Variable to hold birth year input startingSalary = 0 #Variable to hold starting salary input retirementAge = 0 #Variable to hold retirement age input...

  • USING PYTHON - Write a program that calls a function that prompts the user to enter...

    USING PYTHON - Write a program that calls a function that prompts the user to enter a real number. The function should verify that the user entered a valid real number, and should prompt the user to re-enter any invalid input. After a valid real number has been entered, the function should return the number as a number. To test your function, from a main function, call the function two separate times and print the sum of the two numbers...

  • Write a program(Python language for the following three questions), with comments, to do the following:   ...

    Write a program(Python language for the following three questions), with comments, to do the following:    Ask the user to enter an alphabetic string and assign the input to a variable str1. Print str1. Check if str1 is valid, i.e. consists of only alphabetic characters. If str1 is valid Print “<str1> is a valid string.” If the first character of str1 is uppercase, print the entire string in uppercase, with a suitable message. If the first character of str1 is...

  • JAVA only Please do and post Code with Screenshot. Write a program that reads a paragraph...

    JAVA only Please do and post Code with Screenshot. Write a program that reads a paragraph from user and extract and print out any telephone number in the text. The telephone numbers can have the following formats. For this assignment you need to create objects from class Pattern and Matcher. (xxx) xxx-xxxx xxx-xxx-xxxx

  • Python code

    Write a Python program that takes the value of n from the user and calculates the value of S using the following equation:S = 1! - 2! + 3! - 4! + 5! - 6! + ……..  n!Here, the value of n should be a positive (n>0) integer. If the user gives a negative input or zero, then print “Invalid Input!”. Also, print the sentence “End of program.” in the last line no matter what the input is.---------------------------------------------------------------------Sample Input 1:4Sample Output 1:Value of...

  • Write a python program that does the following. a. It asks the user to enter a...

    Write a python program that does the following. a. It asks the user to enter a 5-digit integer value, n. b. The program reads a value entered by the user. If the value is not in the right range, the program should terminate. c. The program calculates and stores the 5 individual digits of n. d. The program outputs a “bar code” made of 5 lines of stars that represent the digits of the number n. For example, the following...

  • use python IDEL Please highlight the answer Problem 1 Errors and Exceptions. There are two main...

    use python IDEL Please highlight the answer Problem 1 Errors and Exceptions. There are two main types of errors: syntax errors and runtime errors. Syntax errors are detected by the Python interpreter before the program execution during the parsing stage (parsing is a process, during which the Python interpreter analyzes the grammatical structure of the program). Runtime errors are errors in a program that are not detected by the Python interpreter during its parsing stage. They are further divided into...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT