160k views
2 votes
Convert the following if-else into ternary if-else statement.

-------------
tomorrow = "rain"
play = "I will play football tomorrow for sure"
not_play = "I am not going to play football tomorrow"
my_decision = None
if tomorrow == "rain":
my_decision = not_play
else:
my_decision = play
print(my_decision)
---------------

1 Answer

5 votes

Final answer:

To convert the given if-else statement into a ternary if-else statement, use the format condition ? expression1 : expression2. In this case, the condition is tomorrow == 'rain', expression1 is not_play, and expression2 is play. The converted code would be my_decision = not_play if tomorrow == 'rain' else play.

Step-by-step explanation:

To convert the given if-else statement into a ternary if-else statement, we can use the following format: condition ? expression1 : expression2.

In this case, the condition would be tomorrow == 'rain'. The expression1 will be not_play, and the expression2 will be play.

The converted code would look like this: my_decision = not_play if tomorrow == 'rain' else play.

This ternary if-else statement assigns not_play to my_decision if tomorrow is equal to 'rain', and assigns play otherwise.

User Emmanu
by
7.7k points

Related questions

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.