-2

I have a case where I have to use if statement learning python:

if ([Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and
 Team1_matches[3]>Team2_matches[3] and Team1_matches[4]>Team2_matches[4]]):
winner="Team 1"
elif ([Team2_matches[0]>Team1_matches[0] and Team2_matches[1]>Team1_matches[1] and Team2_matches[2]>Team1_matches[2] and
  Team2_matches[3]>Team1_matches[3] and Team2_matches[4]>Team1_matches[4] ]):
winner="Team 2"
else:winner="it was a draw or something went wrong"

It's always returning team 1 for some reason due to my poor combination, please advice how I can achieve the real value for winner, without having to if for many lines, if this is programmatically correct I will do it, just need advice

Without the braces

   if Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and
                                                                                                                    ^

SyntaxError: invalid syntax

I am getting the input this way:

Team1_matches[0] = input("Enter the score that team 1 got on match 1? ")
Ridah
  • 39
  • 8
  • what is the input? why are there `[]` around your if conditions? – depperm Feb 23 '17 at 19:35
  • 4
    Remove the braces around your if condition. `if ([...]):` -> `if ...:`. As it is, you're checking the truth value of a list of booleans, which is always `True` because it's not empty. – Aran-Fey Feb 23 '17 at 19:36
  • You should also look into [how to iterate through two lists in parallel](http://stackoverflow.com/questions/1663807/how-can-i-iterate-through-two-lists-in-parallel-in-python). – Random Davis Feb 23 '17 at 19:41
  • Also, you should not ever post code in comments. – Random Davis Feb 23 '17 at 19:41

2 Answers2

1

As Rawing said in the comments, you have unneeded braces around your checks and non-empty lists are considered True. See the Python documentation for more info.

Try this:

Team1_matches=[1,2,3,4,5]
Team2_matches=[5,5,5,5,6]
if (Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and Team1_matches[3]>Team2_matches[3] and Team1_matches[4]>Team2_matches[4]):
    winner="Team 1"
elif (Team2_matches[0]>Team1_matches[0] and Team2_matches[1]>Team1_matches[1] and Team2_matches[2]>Team1_matches[2] and Team2_matches[3]>Team1_matches[3] and Team2_matches[4]>Team1_matches[4]):
    winner="Team 2"
else:
    winner="it was a draw or something went wrong"

print(winner)

>>> Team 2
Avantol13
  • 1,009
  • 11
  • 21
  • Thank you, so what's the best way or value to put for the lists? Or is there any logic behind the numbers you filled the lists with? – Ridah Feb 23 '17 at 19:56
  • That's up to your implementation. I just put in values to test. How are you getting these lists? – Avantol13 Feb 23 '17 at 19:58
  • Sorry for that, will add it in the first post in a second. – Ridah Feb 23 '17 at 19:59
1

The solution using built-in all function:

Team1_matches = [1,2,3,4,5]
Team2_matches = [2,3,4,5,6]

r = range(0, len(Team1_matches))  # range size

if all(Team1_matches[i] > Team2_matches[i] for i in r):
    winner="Team 1"
elif all(Team2_matches[i] > Team1_matches[i] for i in r):
    winner="Team 2"
else:
    winner="it was a draw or something went wrong"

print(winner)  # outputs: "Team 2"
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105