# Boolean conditional function

I would like to construct a function with return the string "Vrai" or the string 'Faux' selon qu $x>y$ ou $x<=y$. I have tried

cond(x, y)= if bool(x<y)='True' then Print("Accepté") else Print("Rejeté")


but it leads to an error

edit retag close merge delete

Could you please edit the question to make it more consistent:

• decide between Vrai/Faux vs Accepté/Rejeté
• "a function with" -> "a function which"
• selon que ... ou ... -> according to whether ... or ...
• decide whether it's "x > y" or "x < y" that should be "accepted"

I am confused to be able to write such inconsistent english

Sort by » oldest newest most voted

To construct a function, you have to use def Python statement.

def cond(x, y):
if x<y:
print("Accepté")
else:
print("Rejeté")


EDIT given the comment, it seems that you do not want to only print the result, but you want the function to return the string. So, here it is:

def cond(x, y):
if x<y:
return "Accepté"
else:
return "Rejeté"


All this is pure Python. Let me suggest to read some introductions about this generic language, since Sage is based on it, and you will benefit a lot to make clever constructions out of Sage mathematical bricks.

more

A subsequent consequence of your formula. I have an error with the following code

a=1
b=2
c=3
d=4
e=5
f=6
essay = [[cond(a,b)],[cond(c,d)],[cond(e,f)]]


and when it works results are not displayed in the table

In some cases a lambda function is also okay.

It takes up less lines than a def function.

sage: cond = lambda x, y: "Accepté" if bool(x<y) else "Rejeté"


Then:

sage: a, b, c, d, e, f = [1 .. 6]
sage: essay = [[cond(a, b)], [cond(c, d)], [cond(e, f)]]
sage: hc = ['test', r'A', r'B', r'C']
test | résultat
+------+----------+
A    | Accepté
B    | Accepté
C    | Accepté

more