0
How to display the output of a ternary operation next to a string, using Python?
I intend to check if the size of the name typed is larger than "Braga". For this condition, I want to use the ternary Python operator to check whether it is "larger" or "smaller" and join this information in the final print string: "Its name is bigger/ smaller than mine!".
def operadorTernario():
""" Exemplo do operador ternário """
nome = input('Qual o seu nome? ')
print('O seu nome é', 'maior' if len(nome) > 'Braga' else 'menor', 'que o meu!')
operadorTernario()
To test online, recommend: Repl.it
"Larger than Braga" makes no sense. Would be bigger than the size of Braga? If yes, should be
len('Braga')
, or only 5 even, since that value would not change.– Woss
exactly, as @Andersoncarloswoss you are comparing the size of the name typed with the word Raga...
– Anderson Henrique
would be a common if and E-if, if it is larger print than, smaller print case than
– Victor
Lucas, you can use the button share from Repl.it to share your code working.
– Woss
@Andersoncarloswoss refers to the size of "Braga". I understood what actually showed the error output in the terminal. Erroneously I was returning a
int
of the name received in the input and trying to compare with the string "Braga". I’ve fixed using thelen('Braga')
. Thanks!– Lucas Braga