mardi 26 juillet 2016

Python: Shorten concatenation by using inline conditionals

If the title is a bit obscure, an example of what I would like to do is:

print("Status: " + if serverIsOnline: "Online" else: "Offline")

I know this is improper, but what I am trying to do is check if serverIsOnline is True then print Status: Online else Status: Offline. I know it's possibe, I have seen it done, but I can't remember how it was done.

This is a shorter equivalent of:

if serverIsOnline:
    print("Status: Online")
else:
    print("Status: Offline")

Could someone please refresh me?

Edit: I always research my questions to the best of my abilities before I ask a question on StackOverflow.

As it is with this question. None of the google search terms I used yielded any results, and I wasn't about to go searching through every Python forum to find an answer to a question which could easily be asked on StackOverflow, especially when this is most likely going to be useful for other users in the future.

Aucun commentaire:

Enregistrer un commentaire