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 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...

  • 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 PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

  • 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 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...

  • Can someone please rewrite this code using a while loop? Please write it in python. #The...

    Can someone please rewrite this code using a while loop? Please write it in python. #The winning number my_number = 12 #variable for the input name user_n = input("Hello! What is your name?") #Ask the user print("Well",user_n,": I am thinking of a number between 1 and 20." + "Take a guess.") user_n = float(input("Take a guess.")) #if statements if user_n == my_number: print("Good job" ,user_n, "You guessed my number!") if user_n > my_number: print("Your guess is high. You lose.") if...

  • Write a python code that takes in an number 0-9 and prints out the word of...

    Write a python code that takes in an number 0-9 and prints out the word of the number. For example 1 would print out one. Below is the skeleton of the code that needs to be filled in. def num2string(num): """ Takes as input a number, num, and returns the corresponding name as a string. Examples: num2string(0) returns "zero", num2string(1)returns "one" Assumes that input is an integer ranging from 0 to 9 """ numString = "" ################################### ### FILL IN...

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • For this lab, modify the python code below so it will continuously be asking user for...

    For this lab, modify the python code below so it will continuously be asking user for inputs to calculate the interest earned until the user enters number 0 (zero) in principle. Once the user enters any number less or equal to 0 in principle, the program will end. here is my code: # Programming Exercise 3-14 # Local variables p = 0.0 r = 0.0 n = 0.0 t = 0.0 loop = True while loop: p = float(input('\nEnter the...

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
ADVERTISEMENT