The program accepts two input values, adds them and gives the amount to the user.
Before folding using the isdigit() function, isdigit() is checked that all the entered characters are numbers and numbers are separated using .split() to perform the increment operation of the result value. If the presence of extraneous characters is detected, an error message is displayed and the function is restarted.
If you enter numbers that contain more than one character, the numbers are not combined, as a result of which an error occurs (in the if-else )
I do not know how to solve the problem. It is advisable to do without separate input of each number and checking for it and WITHOUT using try-except , if possible. Thank you in advance.
Source:
def sum(): num=input('enter two numbers with space') '''ввод двух исел через пробел''' result=0 '''присваивается результат суммы введенных чисел''' num = '0'.join(num) '''объединение введенных чисел через 0 ( ноль )''' if num.isdigit() == True: '''проверка на отсутствие символов кроме чисел''' '''разделение чисел по знаку 0''' num = num.split('0') '''увеличение result на значение введенных чисел''' for i in num: i=int(i) result+=i else: '''вывод сообщения об ошибке и запуск функции по новой''' print('you should enter only numbers') sum() '''вывод суммы''' print(result) '''начальный вызов функции''' sum()