Reputation: 89
Say, for example, that there are three football teams whose players all need to sign in each day using a Python 3.x program shared between them.
Which of these would be more efficient/better and why?
This:
while True:
team = input("Which team are you in? A, B or C?").lower()
if team == "a" or team == "b" or team == "c":
#asks for player's number and then registers him in for their team for the day
break
print("Invalid input. Please enter A, B or C.")
or this:
while True:
team = input("Which team are you in? A, B or C?").lower()
if team == "a":
#asks for player's number and then registers him in for their team for the day
break
if team == "b":
#asks for player's number and then registers him in for their team for the day
break
if team == "c":
#asks for player's number and then registers him in for their team for the day
break
print("Invalid input. Please enter A, B or C.")
Upvotes: 0
Views: 399
Reputation: 3405
The first one is concise and right for only obtaining valid input. The second one is when there is some preprocessing to do for each valid input before actually using the input, as you are still in the while True
loop. You can make the 1st one cleaner,
options = ("a", "b", "c")
while True:
option = input("x: ")
if option in options:
break
Upvotes: 0
Reputation: 31885
if Efficient/better means which way runs faster and you concern the performance of your code, you can write a simple benchmark to test it in your case. You can calculate the elapsed time between two time points. For example checking 100K times for a random team
value.
From my own perspective, I think we should write more readable code, makes the code much easier to be understood by other programmers, it may not speed up your developing, but it will not delay your project for sure.
Upvotes: 0
Reputation: 4379
The first approach is better because it's easier to read. The second approach would be better if you needed to do different checks per team. Since you are writing separate ifs you have more flexibility.
In terms of efficiency it really maters what you would do inside the if statement afterwards. E.g. if in the first case you test if team in ('a', 'b', 'c') and when true, you search again in a list to find the exact variables to work on, whereas in the second case since you know the exact team you don't need to search and process those variables directly, then the second approach will be more efficient.
To get the best of both, clean approach that searches only once you could write something like this:
team_objects = { # Or db search instead
'a': team_a,
'b': team_b,
'c': team_c,
}
letter = input('Which team are you in? A, B or C?').lower()
team = team_objects.get(letter, None)
if team:
team.players += 1 # or something else
else:
print('Invalid input. Please enter A, B or C.')
Upvotes: 0
Reputation: 107287
The difference is that when you use separate if
statement for your conditions you would be able to follow an special code block (commands) based on that condition, while if you put all of the conditions on one if
statement you have to use same commands for all of them.
So it's all depends on your logic, and there is not difference in terms of performance. if you want to follow an specific command for all of the conditions you better to chain the conditions with boolean operators. Otherwise you should use separate if
statements for each condition and follow the related commands for each condition.
Upvotes: 4