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!")

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter6: Using Arrays
Section: Chapter Questions
Problem 2CP
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
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT