Python. I cannot return an if statement within a function

Asked

Viewed 102 times

-4

def func(x=1, y=3):

  if x == y:
  return "Igualdade."
  else: 
    return "Desigualdade."
  if x > y or x < y:
    return True
  if True == x > y:
    return x
  else:
    return y
if x < y:
  return (y - x) == c
  if c + x == y:
      return "Agora está igual!"
  else:
    return "Afinal, não está igual."

It’s already solved. Thank you all for your help, here’s how the code looks. Note: I took it out of function.

x = 1
y = 3

if x == y:
  print("Igualdade.")
else: 
  print("Desigualdade.") 
if x > y or x < y:
  print(True)
if True == x > y:
  print("x", x)
else:
  print("y", y)
if x < y:
 print(y - x) 
if y - x + x == y:
  x = y - x + x
if x == y:
  print("Agora está igual!")

I moved a bit more and now adding 'Else', 'and' and 'not'.

if x == y:
  print("Igualdade.")
else: 
  print("Desigualdade.") 
if x > y and not x < y:
  print("x", x)
else:
  print("y", y)
if x < y:
  print(y - x) 
if y - x + x == y:
  x = y - x + x
if x == y:
  print("Agora está igual!")

Other option.

if x == y:
  print("Igualdade.")
else: 
  print("Desigualdade.") 
if x > y and not x < y:
  print("x", x)
else:
  print("y", y)
if x > y:
  y = (x - y) + y
else:
  x = (y - x) + x 
if x == y:
  print("Agora está igual!")
  • 1

    Could explain what you’re trying to do?

  • Hello, friend, I was wanting to return the message, at the end of the code. Where it says, "After all, it’s not the same". But I already got it. Instead of explaining, I’ll post the code here for you to see how it turned out.

  • x = 1 y = 3 if x == y: print("Equality.") Else: print("Inequality.") if x > y or x < y: print(True) if True == x > : print("x", x) Else: print("y", y) if x < y: print(y - x) if y - x + x == y: x = y - x + x if x == y: print("Now it’s the same!")

  • Very good, thank you.

  • Thank you. Hug friend.

1 answer

1

By your code only the first 4 lines of the function are doing something. The command Return exits the function and the first if is speaking exits the function with the equal value if x is equal to y, or exits the function with inequality if x is not equal to y, the rest of the function is never executed.

  • Hello, it is, I noticed the error. Although I corrected before seeing your comment, thank you, still helped me. Hug friend.

Browser other questions tagged

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