#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    58
    Rep Power
    3

    Getting error in my code !


    Hi,

    I'm running the following code and getting "Syntax Error: Invalid Syntax " in the following code. Please let me know where I'm wrong.

    Here is the code:

    Code:
    import random
    
    howMany = random.randint(0,1000)
    stats = {}
    for i in range(howMany):
       value = random.randint(0,500)
       stats.setdefault(value,0)
       stats[value]+=1
    for item in stats:
       if stats[item] > 1:
           print item
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    If you are getting a syntax error it is because you are using python 3. You need to place parenthesis around your print statement as print is a function in py3.

    python Code:
    import random
     
    howMany = random.randint(0,1000)
    stats = {}
    for i in range(howMany):
       value = random.randint(0,500)
       stats.setdefault(value,0)
       stats[value]+=1
    for item in stats:
       if stats[item] > 1:
           print(item)

    Now whether or not your code is doing what you want or not is a different story.

    -Mek
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    58
    Rep Power
    3
    Thanks for your reply. Yes, when i checked it on Python 2.4 it worked fine.

    Could you tell me what does this code actually do?

    Originally Posted by Mekire
    If you are getting a syntax error it is because you are using python 3. You need to place parenthesis around your print statement as print is a function in py3.

    python Code:
    import random
     
    howMany = random.randint(0,1000)
    stats = {}
    for i in range(howMany):
       value = random.randint(0,500)
       stats.setdefault(value,0)
       stats[value]+=1
    for item in stats:
       if stats[item] > 1:
           print(item)

    Now whether or not your code is doing what you want or not is a different story.

    -Mek
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    58
    Rep Power
    3
    Also, I didn't see any array definition like
    Code:
     stats = {}
    in python doc. Is it a correct way to declare an array?

    Originally Posted by Jack_Tauson_Sr
    Thanks for your reply. Yes, when i checked it on Python 2.4 it worked fine.

    Could you tell me what does this code actually do?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Code:
    stats = {}
    Creates an empty dictionary, sometimes referred to as an "associative array" (though not amongst python programmers). It is an unordered key/value mapping.
    http://docs.python.org/2/tutorial/da...l#dictionaries

    The Python datatype closest to a standard array is a list. An empty list is declared:
    Code:
    some_list = []
    As for your code, it generates a random number between 0 and 1000. Then it generates a random number between 0 and 500 that many times. The number of times each number appears is kept track of in the dictionary. Then at the end any numbers in the dictionary that appeared more than once are printed.

    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo