140k views
4 votes
Write a program that prompts the user to enter two integers, and then calculates and prints the quotient of the two integers. Use a try-except block to handle the case where the user enters O as the second integer.

Write a function that takes a string as input and raises a ValueError exception if the string contains any non- alphabetic characters (i.e., anything other than letters). Otherwise, the function should return the input string.

1 Answer

4 votes

Final answer:

To write a program that prompts the user to enter two integers, calculates their quotient, and handles the case where the second integer is zero, use a try-except block. To write a function that raises a ValueError if a string contains non-alphabetic characters, use the isalpha() method.

Step-by-step explanation:

To write a program that prompts the user to enter two integers and calculates and prints their quotient, you can use the following Python code:

try:
num1 = int(input('Enter the first integer: '))
num2 = int(input('Enter the second integer: '))
quotient = num1 / num2
print('The quotient of', num1, 'and', num2, 'is', quotient)
except ZeroDivisionError:
print('Error: Cannot divide by zero')

To write a function that raises a ValueError if a string contains any non-alphabetic characters, you can use the following Python code:

def check_string(input_string):
if not input_string.isalpha():
raise ValueError('Error: Non-alphabetic characters found')
return input_string

User Stranger
by
8.0k points