I have inlcluded my code and cannot get the y or no portion to add at end of the loop and trying to understand it. Thank you !   # Define a function to perform safe division def safe_division(num1, num2):     try:         result = num1 / num2         return result     except ValueError:         return "Invalid input. Please enter valid numbers."     except ZeroDivisionError:         return "Division by zero is not allowed." # Create a while loop to allow the user to continue trying while True:     try:         # Prompt the user for the first number         input_num1 = input("Enter the first number: ")                           # Convert the user input to a floating-point number         num1 = float(input_num1)                  # Prompt the user for the second number         input_num2 = input("Enter the second number: ")                   # Convert the user input to a floating-point number         num2 = float(input_num2)                  # Call the safe_division function and print the result         result = safe_division(num1, num2)         print(f"Result: {result}")     except ValueError:         print("Error - Both inputs must be numbers.")     except ZeroDivisionError:         print("Error - Cannot divide by zero.")                  # Add to try again               print("Thank you for using Safe Division Program!")

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

I have inlcluded my code and cannot get the y or no portion to add at end of the loop and trying to understand it.

Thank you !

 

# Define a function to perform safe division
def safe_division(num1, num2):
    try:
        result = num1 / num2
        return result
    except ValueError:
        return "Invalid input. Please enter valid numbers."
    except ZeroDivisionError:
        return "Division by zero is not allowed."

# Create a while loop to allow the user to continue trying
while True:
    try:
        # Prompt the user for the first number
        input_num1 = input("Enter the first number: ")
        
        
        # Convert the user input to a floating-point number
        num1 = float(input_num1)
        
        # Prompt the user for the second number
        input_num2 = input("Enter the second number: ")     
    

        # Convert the user input to a floating-point number
        num2 = float(input_num2)
        
        # Call the safe_division function and print the result
        result = safe_division(num1, num2)
        print(f"Result: {result}")
    except ValueError:
        print("Error - Both inputs must be numbers.")
    except ZeroDivisionError:
        print("Error - Cannot divide by zero.")
        
        # Add to try again
             
print("Thank you for using Safe Division Program!")

Shell
>>> Run PS5_P1_solution.py
Safe Division Program
Enter the first number: 10
Enter the second number: 2
Result: 5.0
Would you like to try again? (y/n): y
Enter the first number: 33.13
Enter the second number: 11.111
Result: 2.981729817298173
Would you like to try again? (y/n): y
Enter the first number: 10
Enter the second number: 0
Result: Error - Cannot divide by zero..
Would you like to try again? (y/n): y
Enter the first number: 8
Enter the second number: four
Result: Error - Both inputs must be numbers.
Would you like to try again? (y/n): n
Thank you for using the Safe Division Program!
Transcribed Image Text:Shell >>> Run PS5_P1_solution.py Safe Division Program Enter the first number: 10 Enter the second number: 2 Result: 5.0 Would you like to try again? (y/n): y Enter the first number: 33.13 Enter the second number: 11.111 Result: 2.981729817298173 Would you like to try again? (y/n): y Enter the first number: 10 Enter the second number: 0 Result: Error - Cannot divide by zero.. Would you like to try again? (y/n): y Enter the first number: 8 Enter the second number: four Result: Error - Both inputs must be numbers. Would you like to try again? (y/n): n Thank you for using the Safe Division Program!
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Types of Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education