By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

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

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

Invalid syntax: print variation_string

Expand|Select|Wrap|Line Numbers
  1.  
  2. import itertools
  3.  
  4. variations = itertools.product('ab', repeat=4)
  5. for variations in variations:
  6.         variation_string = ""
  7.         for letter in variations:
  8.                 variation_string += letter
  9.         print variation_string
  10.  
  11.  
  12.  
Jan 17 '11 #1

✓ answered by dwblas

What version of Python are you using? If it is 3.x, then print is a function:
print(variation_string)
Expand|Select|Wrap|Line Numbers
  1. 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).

Share this Question
Share on Google+
3 Replies


100+
P: 332
Read your line 5 again. How about
For variation in variations
Then change line 7 accordingly.
Jan 17 '11 #2

Expert 100+
P: 621
What version of Python are you using? If it is 3.x, then print is a function:
print(variation_string)
Expand|Select|Wrap|Line Numbers
  1. 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).
Jan 17 '11 #3

P: 6
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

Expand|Select|Wrap|Line Numbers
  1. import itertools
  2. print('"""')
  3. variations = itertools.product('ab', repeat=4)
  4. for variation in variations:
  5.     # print( variation )
  6.     variation_string = ""
  7.  
  8.     for letter in variation:
  9.         variation_string += letter
  10.         # print(variation_string)
  11.     print(variation_string)
  12. print('"""')
  13.  
  14. """
  15. aaaa
  16. aaab
  17. aaba
  18. aabb
  19. abaa
  20. abab
  21. abba
  22. abbb
  23. baaa
  24. baab
  25. baba
  26. babb
  27. bbaa
  28. bbab
  29. bbba
  30. bbbb
  31. """
Jan 20 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.