#python
Вопрос:
Как настроить ввод пользователями num1 и num2, если он находится в строке с if
/ else
или try
/ except
? Если кто-то вводит нечисловую строку вместо числа в num1
/ num2
, это должно привести к ошибке. Я не знаю, как это проверить. Это то, что у меня есть до сих пор:
print("n Select A Number n")
print("Press 1 for Addition")
print("Press 2 for Subtraction")
print("Press 3 for multiplication")
print("Press 4 for Division n")
while True:
user = input("Enter A Choice(1/2/3/4): ")
if user in ('1','2','3','4'):
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
if user == '1':
print(f"{num1} {num2} = {num1 num2}")
print("Thanks For Usnig This Calculator")
elif user == '2':
print(f"{num1} - {num2} = {num1 - num2}")
print("Thanks For Usnig This Calculator")
elif user == '3':
print(f"{num1} * {num2} = {num1 * num2}")
print("Thanks For Usnig This Calculator")
elif user == '4':
print(f"{num1} / {num2} = {num1 / num2}")
print("Thanks For Usnig This Calculator")
else:
print("Please Enter Correct Choice")
else:
print("Invalid")
Ответ №1:
вы можете использовать try...except ValueError
, как показано ниже:
print("n Select A Number n")
print("Press 1 for Addition")
print("Press 2 for Subtraction")
print("Press 3 for multiplication")
print("Press 4 for Division n")
while True:
user = input("Enter A Choice(1/2/3/4): ")
if user in ('1','2','3','4'):
try:
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
if user == '1':
print(f"{num1} {num2} = {num1 num2}")
print("Thanks For Usnig This Calculator")
elif user == '2':
print(f"{num1} - {num2} = {num1 - num2}")
print("Thanks For Usnig This Calculator")
elif user == '3':
print(f"{num1} * {num2} = {num1 * num2}")
print("Thanks For Usnig This Calculator")
elif user == '4':
print(f"{num1} / {num2} = {num1 / num2}")
print("Thanks For Usnig This Calculator")
else:
print("Please Enter Correct Choice")
except ValueError:
print("please enter int")
else:
print("Invalid")
Ответ №2:
Попробуй это
print("n Select A Number n")
print("Press 1 for Addition")
print("Press 2 for Subtraction")
print("Press 3 for multiplication")
print("Press 4 for Division n")
while True:
user = input("Enter A Choice(1/2/3/4): ")
if user in ('1','2','3','4'):
try:
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
if user == '1':
print(f"{num1} {num2} = {num1 num2}")
print("Thanks For Using This Calculator")
elif user == '2':
print(f"{num1} - {num2} = {num1 - num2}")
print("Thanks For Using This Calculator")
elif user == '3':
print(f"{num1} * {num2} = {num1 * num2}")
print("Thanks For Using This Calculator")
elif user == '4':
print(f"{num1} / {num2} = {num1 / num2}")
print("Thanks For Using This Calculator")
else:
print("Please Enter Correct Choice")
except ValueError:
print("Not a number sucka, type again properly")
else:
print("Invalid")
Это основной блок try catch.
Идите сюда, если хотите узнать больше
https://www.tutorialspoint.com/python3/python_exceptions.htm#:~:text=except ExceptionType as Argument: You,the tuple of the exception.