while browsing codegolf, found this :
g=input();print("approved","summer school","failed")[(g<7)+(g<3)]
i don't understand [] means after print()... clarification ?
>>> print("approved","summer school","failed")[0] approved >>> print("approved","summer school","failed")[1] summer school >>> print("approved","summer school","failed")[2] failed
see pattern? it's simple indexing.
by way know (g<7)+(g<3)
>= 0
(false + false
) , <= 2
(true + true
).
recall bool
subclass of int
, , true == 1
, false == 0
(which why can add bool
s, done above).
finally, goes without saying confusion have been avoided set of parenthesis:
print(("approved","summer school","failed")[(g<7)+(g<3)])
Comments
Post a Comment