-4
def par_ou_impar(result):
divided = str(result).split('.')
if len(divided) < 2:
if divided[0] % 2 == 0:
print('O resultado é par')
else:
print('O resultado é ímpar')
if int(divided[1]) % 2 == 0:
print('O resultado é par')
else:
print('O resultado é ímpar')
is_float = True
try:
numero1 = float(input('Digite o primeiro número:'))
except Exception:
is_float = False
while not is_float:
print('Favor digitar um número válido')
try:
numero1 = float(input('Digite o primeiro número:'))
is_float = True
except Exception:
is_float = False
is_float = True
try:
numero2 = float(input('Digite o segundo número:'))
except Exception:
is_float = False
while not is_float:
print('Favor digitar um número válido')
try:
numero2 = float(input('Digite o segundo número:'))
is_float = True
except Exception:
is_float = False
print('Info.: Número1 () Número2')
print('1 para soma')
print('2 para subtração')
print('3 para multiplicação')
print('4 para divisão')
is_int = True
try:
operacao = int(input('Digite a operação que você quer realizar:'))
if 5 > operacao > 0:
is_int = True
else:
is_float = False
except Exception:
is_int = False
while not is_int:
print('Favor digitar um valor válido inteiro')
try:
operacao = int(input('Digite a operação que você quer realizar:'))
if 5 > operacao > 0:
is_float = True
else:
is_float = False
except Exception:
is_int = False
if operacao == 1:
result = numero1 + numero2
elif operacao == 2:
result = numero1 - numero2
elif operacao == 3:
result = numero1 * numero2
elif operacao == 4:
result = numero1 / numero2
if result % int(result) == 0:
print('O resultado é:', int(result))
else:
print('O resultado é:', result)
if result >= 0:
print('O resultado é positivo')
else:
print('O resultado é negativo')
if round(result) == result:
print('O resultado é inteiro')
else:
print('O resultado é decimal')
par_ou_impar(result)
You’re making this mistake:
Digite o primeiro número:2
Digite o segundo número:3
Info.: Número1 () Número2
1 para soma
2 para subtração
3 para multiplicação
4 para divisão
Digite a operação que você quer realizar:6
Traceback (most recent call last):
File "C:/Users/AppsTo/PycharmProjects/EstruturaDeDecisão/Exec24.py", line 81, in <module>
if result % int(result) == 0:
NameError: name 'result' is not defined
Process finished with exit code 1
But it should not reach the result if the operacao is not working.
You typed Operation 6, it won’t enter any
iforelifthat initialize the variableresult, qnd tries to access the variable that was not initialized gives error.– Math
Yes, but that’s what this is for... Try: operation = int(input('Type the operation you want to perform:')) if 5 > operation > 0: is_int = True Else: is_float = False
– Igor Munhoz
In this case then you missed the variable name inside the
elseof the firsttry catchand also in theifandelsein the secondtry catch, should beis_intand notis_float– Math
Yeah, that’s right. Now I just need to test if he took the float
– Igor Munhoz
It was still a mistake, at the time of making the float.. Look at this. Type the first number:3 Type the second number:5 Info.: Number1() Number2 1 for sum 2 for subtraction 3 for multiplication 4 for division Enter the operation you want to perform:4 Traceback (Most recent call last): File "C:/Users/Appsto/Pycharmprojects/Structuredesign/Exec24.py", line 81, in <module> if result % int(result) == 0: Zerodivisionerror: float modulo ?
– Igor Munhoz
if result % int(result) == 0: print('The result is:', int(result)) Else: result = float(round(result, 4)) print('The result is:', float(result)) Tidy like this and giving problems See: 2 Type the second number:8 Info.: Number1() Number2 1 for sum 2 for subtraction 3 for multiplication 4 for division Enter the operation you want to perform:4 Traceback (Most recent call last): File "C:/Users/Appsto/Pycharmprojects/Structuredesign/Exec24.py", line 81, in <module> if result % int(result) == 0: Zerodivisionerror: float module
– Igor Munhoz