Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do i fix the function in the condition?

0 votes
asked Mar 7 by Silv5r D4gger (180 points)
Nfração1 = int(input ("Digite o numerador da 1° fracao: "))
Nfração2 = int(input ("Digite o denominador da 1° fracao: "))

Dfração1 = int(input ("Digite o numerador da 2° fracao: "))
Dfração2 = int(input ("Digite o denominador da 2° fracao: "))

Operação = input ("Escolha a operação: a) Adição b) Subtração c) Multiplicação d) Divisão: ")

def Multiplicação():
    print(f"{Nfração1 * Nfração2} / {Dfração1 * Dfração2}")

def Divisão():
    print(f"{Nfração1 * Dfração2} / {Nfração2 * Dfração1}")

def AdiçãoDenominadoresIguais:
    print(f"{Nfração1 + Nfração2} / {Dfração1}")
    
def SubtraçãoDenominadoresIguais:
    print(f"{Nfração1 - Nfração2} / {Dfração1}")

if Operação == "d" or "D":
    Divisão()

elif Operação == "c" or "C":
    Multiplicação()

When i run the code, even if i press C for Multiplicação, is the Divisão fuction that runs instead of the Multiplicação, what do i do?

1 Answer

0 votes
answered Mar 7 by Peter Minarik (86,240 points)

Your code does not compile. You need parentheses after function names for the declarations:

def AdiçãoDenominadoresIguais():
    print(f"{Nfração1 + Nfração2} / {Dfração1}")
    
def SubtraçãoDenominadoresIguais():
    print(f"{Nfração1 - Nfração2} / {Dfração1}")

After this fix, your code compiles. Also, it seems to work just fine for me. Can you provide inputs that don't seem to work for you for the code above (not any other version, I can only see the version you shared)?

commented Mar 7 by Silv5r D4gger (180 points)
so, in the Operação, when i type C, the Multilplicação should active instead of the Divisão, this is the problem
commented Mar 8 by Peter Minarik (86,240 points)
You need to do comparisons (Operação == "D") against both lower case and upper case versions of the keys, not just say `or "D"`

if Operação == "d" or Operação == "D":
    Divisão()

elif Operação == "c" or Operação == "C":
    Multiplicação()

Now with this change, you should be OK.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...