Here is a plan for a (relatively) random numeric dictionary I've put together. I'm hoping that people can help me refine the plan so that I can start incremental coding:

#! usr/bin/env python

# creates a random dictionary

from random import randint

# user inputs maximum length of dictionary
    # program chooses a random length of max-(randint(1,max-1))

# user inputs maximum lengths of keys and values

# for each dictionary entry:
    # program establishes key and value lengths of\ 
      max-randint(1,max-1)
    # program generates randint values for key and value\
      within length parameters
    # program converts value to string if necessary

# program generates a dictionary of given length using\
  the given keys and values

Recommended Answers

All 3 Replies

I don't know how others are faring here, but every time I try to post something I get an error message complaining my code is improperly formatted. Even if it's all comments! After which I'm often no longer able to type into the editing box.

Something is clearly hosed. Is it possible to disabe the nanny syntax checker, since it's not working? [OK, I think it's not working] Frankly I think it should be replaced with a check option.

[code=Python]

Please do not use "max" as a variable name. Python lets you do it,
but it's generally a bad idea to overload built-in functions.
#
I would probably start off with something like this.
Save the trivial user input stuff for later.

[/code]

Wow, posting via Opera (instead of Firefox loaded with addons) seems to work. Is the output approximately what you were looking for? If not, perhaps you could clarify what you were after...

# Please do not use "max" as a variable name. Python lets you do it, 
#  but it's generally a bad idea to overload built-in functions.
#
# I would probably start off with something like this.
# Save the trivial user input stuff for later.

import random
rkmax   = 10000      # Random KEY   max value
rvmax   = 100000     # Random VALUE max value
maxent  = 12         # Number of dictionary entries to create
randkey =   lambda : random.randint(1,rkmax)   # KEY-generation function
randvalue = lambda : random.randint(1,rvmax) # VALUE-generation function
mydict  = {randkey(): randvalue() for i in xrange(maxent)} # Generate the dictionary

print mydict    # Have a look
{5857: 4324, 779: 32587, 8676: 83837, 331: 491, 3090: 64330, 6800: 90950, 6481: 87372, 4018: 68669, 9140: 61600, 665: 40635, 5882: 84652, 9532: 74981}

Welcome back BearofNH!

Interesting to know that a loaded down Firefox could give you problems. I use Chrome and have no problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.