Reputation:
while x < len(Hand):
while y < len(Hand):
if Hand[x][0] == Hand[y][0] and y != x:
sameRank += 1
y += 1
x += 1
It highlights a space right before the "if" and says syntax error...Makes no sense.
Upvotes: 7
Views: 28305
Reputation: 15206
I don't see any errors here, but it's possible that you're indenting the block below your if statement too much. Notice that the rest of your program uses 4 spaces to indent? Try reducing the indentation to just 4 spaces and see if it runs.
Your code does have a logic error, however. You won't loop through y for each x if you don't reinitialize y at the start of each x.
Here's the example code I ran with the fix for the logic error:
def example():
Hand = [[1],[2],[3],[3],[4],[5],[2],[2],[1]]
x = 0
sameRank = 0
while x < len(Hand):
y = 0
while y < len(Hand):
if Hand[x][0] == Hand[y][0] and y != x:
sameRank += 1
y += 1
x += 1
if __name__ == "__main__":
example()
Finally, this code can be made a lot more readable by being more "pythonic." Consider this:
def example():
Hand = [[1],[2],[3],[3],[4],[5],[2],[2],[1]]
sameRank = 0
for x in Hand:
for y in Hand:
if x[0] == y[0] and y != x:
sameRank += 1
if __name__ == "__main__":
example()
This code iterates over the contents of Hand
rather than incrementing temporary integer variables which you then use with the index operator. It's better because there are fewer "maintenance" lines (such as x += 1
), it is more readable, and it is more type-insensitive as it will work with any iterable object containing lists.
Or maybe even (per hayden's comment) this:
def example():
Hand = [[1],[2],[3],[3],[4],[5],[2],[2],[1]]
sameRank = sum(1 for x in Hand for y in Hand if x[0] == y[0] and y!=x)
if __name__ == "__main__":
example()
This code combines a call to the sum
function with the generator expression 1 for x in Hand for y in Hand if x[0] == y[0] and y!=x
. The expression returns a generator which yeilds 1 for each item in your list that matches your criteria, and the sum function adds up all these 1's, thereby giving you the value you're after for sameRank
.
Take a look at this article for a good overview of python idioms.
Finally, I'm not sure what editor you're using, but it sounds like it's masking the real problem if you're getting dialog boxes instead of messages and tracebacks straight from the interpreter's stderr/stdout. Sometimes too much help from your editor is a really bad thing when you're trying to learn. I personally use Vim, but this is probably a bit much to ask of a beginner. I don't have much experience with IDLE (it might even be what you're using), but I've heard good things about using it as a learning tool. However if you're doing serious development you'll quickly outgrow it. Either way, if you do use IDLE get used to running your programs from the command line rather than from IDLE itself. I personally find this gives me better feedback in a lot of cases. Finally there's the PyDev IDE (built on Eclipse), which is especially useful for its robust built-in visual debugging. This might be a good choice, but it is indeed a heavyweight option and I'd put it at an "intermediate" level of difficulty to learn if you aren't already familiar with Eclipse. If you are familiar with Eclipse, you'll be right at home with PyDev.
Upvotes: 7
Reputation: 9666
Sometimes a mix of tabs and spaces will trip python up - say, if your while loops are indented with tabs, and your if statements use spaces instead. And although the if statement may now be indented correctly, it may still help to check all of the other lines in your snippet to make sure they're all being indented the same way.
A few ways your editor may be able to help:
\t
, if your editor supports searching by regular expressions) and swap them with four spaces, or vice versa.If your editor supports none of those things, I suggest trying out Programmer's Notepad or Notepad2, if only to help with diagnosing these kinds of issues.
If that sounds unreasonably picky, welcome to programming! It can be frustrating for the best of us. Pick which version you prefer (spaces or tabs), and stick with it. There are also many ways to make this a cleaner piece of code in Python, mentioned above, but it's a good idea to get used to the spacing issues as well.
Upvotes: 0
Reputation: 169535
It's possible you have a space-like character before the if
- I used to get this when using TextMate, where alt+space would insert a non-breaking-space Unicode character instead of a regular space.
Do you have something like # coding: utf-8
at the start of your file? If so, remove it, and you might get a better error message.
Incidentally, without using sum
/generation expressions (which might be more confusing than helpful to something learning Python), your loops can be simplified like so:
for x in range(len(Hand)):
for y in range(len(Hand)):
if Hand[x][0] == Hand[y][0] and y != x:
sameRank += 1
Upvotes: 1