There 's an invalid syntax error but i don't know what's wrong with it.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kakarot vegeta
    New Member
    • Jan 2011
    • 2

    #1

    There 's an invalid syntax error but i don't know what's wrong with it.

    There 's an invalid syntax error but i don't know what's wrong with it.
    the error says

    Invalid syntax: print variation_strin g

    Code:
    import itertools
    
    variations = itertools.product('ab', repeat=4)
    for variations in variations:
            variation_string = ""
            for letter in variations:
                    variation_string += letter
            print variation_string
  • Mariostg
    Contributor
    • Sep 2010
    • 332

    #2
    Read your line 5 again. How about
    For variation in variations
    Then change line 7 accordingly.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      What version of Python are you using? If it is 3.x, then print is a function:
      print(variation _string)
      Code:
      for variations in variations:
      While this is a terrible coding style, it will run as each "variations " points to a different memory address, because for() loop variables are local to the loop (but I wouldn't want to modify or debug it).

      Comment

      • Dev Player
        New Member
        • Jan 2011
        • 6

        #4
        Cut and past the code below and save in a different file; although I see you code in my webbrowser I see that code in the fonts on my system; It's possible the fonts on your system are hiding your syntax error. Although I doubt that.

        Your code works fine on my pc. WinXP Pro-32bit SP3, Python 2.7

        Code:
        import itertools
        print('"""')
        variations = itertools.product('ab', repeat=4)
        for variation in variations:
            # print( variation )
            variation_string = ""
            
            for letter in variation:
                variation_string += letter
                # print(variation_string)
            print(variation_string)
        print('"""')
        
        """
        aaaa
        aaab
        aaba
        aabb
        abaa
        abab
        abba
        abbb
        baaa
        baab
        baba
        babb
        bbaa
        bbab
        bbba
        bbbb
        """

        Comment

        Working...