Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:56:56+00:00 2026-06-12T08:56:56+00:00

while x < len(Hand): while y < len(Hand): if Hand[x][0] == Hand[y][0] and y

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:56:58+00:00Added an answer on June 12, 2026 at 8:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems so dirty emptying a list in this way: while len(alist) > 0
I am using this code: def startThreads(arrayofkeywords): global i i = 0 while len(arrayofkeywords):
Is this conversion right from the original? uint8_t fletcher8( uint8_t *data, uint8_t len )
My code: import heapq def makeHuffTree(symbolTupleList): trees = list(symbolTupleList) heapq.heapify(trees) while len(trees) > 1:
Are they the same thing???? while len(deque)>0: deque.popleft() while deque: deque.popleft() so basically these
I'm having some trouble with syntax options while writing a VBA Macro for Excel.
While debugging and keep pressing F5, if the source code does not exist, eclipse
While trying to build the mysql2 gem with ruby 1.9.2-p320 on Fedora 16, I
While deveoping a site (using Forms authentication and InProc sessionstate) a frequently run into
While developing an application using gwt in ecliplse crashed. Now the server is running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.