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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
Finally, this code can be made a lot more readable by being more “pythonic.” Consider this:
This code iterates over the contents of
Handrather than incrementing temporary integer variables which you then use with the index operator. It’s better because there are fewer “maintenance” lines (such asx += 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:
This code combines a call to the
sumfunction with the generator expression1 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 forsameRank.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.