Форум сайта python.su
a = input() f = float(a.replace(',','.')) b = float(input()) c = input() if b == 0 and (c == 'div' or 'mod' or '/'): print('Деление на 0!') elif c == '+': print(f + b) elif c == '-': print(f - b) elif c == '*': print(f * b) elif c == '/': print(f / b) elif c == 'mod': print(f % b) elif c == 'div': print(f // b) elif c == 'pow': print(f ** b)
if b == 0 and (c == 'div' or 'mod' or '/') and not(c == '-' or '+')
Test input: 5.0 0.0 mod Correct output: Деление на 0! Your code output: Error: Traceback (most recent call last): File "jailed_code", line 16, in <module> print(f % b) ZeroDivisionError: float modulo
Traceback (most recent call last): File "jailed_code", line 18, in <module> print(f // b) ZeroDivisionError: float divmod()
Офлайн
https://www.learnbyexample.org/python-exceptions-try-except/
a = input() f = float(a.replace(',','.')) b = float(input()) c = input() try: if c == '+': print(f + b) elif c == '-': print(f - b) elif c == '*': print(f * b) elif c == '/': print(f / b) elif c == 'mod': print(f % b) elif c == 'div': print(f // b) elif c == 'pow': print(f ** b) except ZeroDivisionError: print('Деление на 0!') except Exception as err: print(err)
Офлайн