What’s the matter? I use Pycharm

Asked

Viewed 151 times

-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.

  • 1

    You typed Operation 6, it won’t enter any if or elif that initialize the variable result, qnd tries to access the variable that was not initialized gives error.

  • 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

  • In this case then you missed the variable name inside the else of the first try catch and also in the if and else in the second try catch, should be is_int and not is_float

  • Yeah, that’s right. Now I just need to test if he took the float

  • 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 ?

  • 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

Show 1 more comment

1 answer

0

I don’t know if this will affect other parts but try to do so.

is_int = True

try:
    operacao = int(input('Digite a operação que você quer realizar:'))
    if 5 > operacao > 0:
        is_int = True
    else:
        is_int = 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_int = True
        else:
            is_int = False

    except Exception:
        is_int = False
  • I don’t understand what you mean friend... the error is at the end.

  • Because in the original, returns the error that result is not defaced, but even spelling it out before will give Zerodivizionerror error. The only way out was to do it there.

  • 1

    I may be wrong, but you copied what I’ve done..

  • But this I already put away, I think I already said that... The problem is where I said at the end. if result % int(result) == 0: print('The result is:', int(result)) Else: result = float(round(result, 4)) print('The result is:', float(result)) I tidied it like this and is giving problems See: Type the first number: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

  • @Igormunhoz I understand, I guess I got confused then.

  • I still think the error is in the result not explained before the decision structure, in its place I would try to use lambda, it would be easier.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.