elo
elo

Reputation: 13

python: proper use of dictionaries

i try to create a function, which generates random int-values and after a value appeared twice the function should return the number of all generated int-values. I have to use a dictionary.

This is my code so far:

def repeat(a,b):
    dict={}
    d=b-a+2
    for c in range(1,d):
        dict['c']=random.randint(a,b)
        for f in dict:
            if dict['f']==dict['c']:
                return c

First problem: It doesn't work.

>>> repeat(1,5)
Traceback (most recent call last):
  File "<pyshell#144>", line 1, in <module>
    repeat(1,5)
  File "<pyshell#143>", line 7, in repeat
    if dict['f']==dict['c']:
KeyError: 'f'

Second problem: if dict['f']==dict['c']: Should be true in first step because both values are the same.

I can't find a smart way to compare all values without comparing a key with itself.

Sorry for my bad english, it's kinda rusty and thank you for your time.

Upvotes: 1

Views: 102

Answers (2)

steveha
steveha

Reputation: 76715

This isn't really an answer to your question. @Lattyware told you the problem. But I can't put code in a comment so I'm posting this as an answer.

Your code is using weird variable names, which makes the code harder to understand. I suggest you use variable names that help the reader to understand the program.

I've changed your variable names and added comments. I also put in a "doc string" but I don't really understand this function so I didn't actually write a documentation message.

def repeat(a,b): # short names are okay for a,b as they are just two numbers
    """
    This is the "doc string".  You should put in here a short summary of what the function
    does.  I won't write one because I don't understand what you are trying to do.
    """
    # do not use built-in names from Python as variable names!  So don't use "dict"
    # I often use "d" as a short name for a dictionary if there is only one dictionary.
    # However, I like @Lattyware's name "stored" so I will use it as well.
    stored={}
    # You only used "d" once, and it's the upper bound of a range; might as well just
    # put the upper bound inside the call to range().  If the calculation was really long
    # and difficult I might still use the variable, but this calculation is simple.

    # I guess you can use "c" in the loop, but usually I use "n" for number if the loop
    # is making a series of numbers to use.  If it is making a series of indexes I use "i".
    for n in range(1,b-a+2):
        stored[n]=random.randint(a,b)
        # Any for loop that loops over a dictionary is looping over the keys.
        for key in stored:
            # I don't understand what you are trying to do.  This loop will always terminate
            # the first time through (when n == 1).  The line above this for loop assigns
            # a value to stored[n], and n will be equal to 1 on the first loop; then this
            # test will trivially succeed.
            if stored[key] == stored[n]:
                return n

Upvotes: 0

Gareth Latty
Gareth Latty

Reputation: 88987

Enclosing your variable names in quotes makes them strings - Python is looking for the key of the letter f, not the key with the integer in the f variable.

Simply use the variable normally and it should work as you expected:

def repeat(a, b):
    stored = {}
    d = b - a + 2
    for c in range(1, d):
        stored[c] = random.randint(a, b)
        for f in stored:
            if stored[f] == stored[c]:
                return c

Note also that you are shadowing the built-in function dict() by naming your variable dict - it is preferable to use another name because of this.

Upvotes: 2

Related Questions