Home > Back-end >  How to fix dictionary not counting duplicate numbers as new entries
How to fix dictionary not counting duplicate numbers as new entries

Time:10-09

Currently, my code is as follows:

max_length = 3

squares = {}

while len(squares) < max_length:
    print("Enter the side length of the square:")
    side = input()
    side = int(side)
    Perimeter = side*4
    Area = side*side

    squares[Area] = Perimeter

And the problem with this is that when you enter the same side length multiple times, it does not count it as a new entry, and instead keeps asking until unique side lengths are entered. Why is this and how can I fix it? If this is not possible with dictionaries, is there any alternative ways of doing this so it does allow duplicate keys and then can be converted to a DataFrame for plotting?

CodePudding user response:

You're going to have to make a counter in the while loop that tracks how many inputs the user gives. Dictionaries don't allow for duplicate entries, so every time you reference a key that is already in the dictionary, you will just overwrite the current value.

ex.

dictionary = {}
dictionary['key1'] = 'value1'
dictionary['key2'] = 'value2'
print(dictionary.items())  
>> dict_items([('key1', 'value1'), ('key2', 'value2')])

now, given the same dictionary, we are going to call the same key: dictionary = {}

dictionary['key1'] = 'value1'
dictionary['key2'] = 'value2'
dictionary['key1'] = 'value3'
print(dictionary.items())  
>> dict_items([('key1', 'value3'), ('key2', 'value2')])

So, as you can see you are just overwriting the value that corresponds to the key. If you want to have your loop stop after 3 inputs, just do something like this:

max_length = 3

squares = {}
number_of_inputs = 0
while number_of_inputs < max_length:
    print("Enter the side length of the square:")
    side = int(input(...))
    
    Perimeter = side*4
    Area = side*side

    squares[Area] = Perimeter
    number_of_inputs  = 1

Otherwise, you are going to need to check if the key already exists and handle the situation from there - not sure your end goal so cant help with that.

CodePudding user response:

This happen because in dictionary it's doesn't allow duplicates keys key should be unique and have one value.

  • To solve this
max_length = 3

    squares = {}

    while len(squares) < max_length:
        side = input("Enter the side length of the square:")
        side = int(side)
        Perimeter = side * 4
        Area = side * side
        if Area in squares.keys():
            print(f"this is side already exist {side} chose differ one")
        else:
            squares[Area] = Perimeter

CodePudding user response:

I have solved it by using a list instead and then using list.append to add new values. Like so

max_length = 3
inputs = 0
squares = []

while inputs < max_length:
    print("Enter the side length of the square:")
    side = input()
    side = int(side)
    Perimeter = side*4
    Area = side*side
    inputs = inputs 1

    squares.append([Perimeter, Area])
print(squares)

This works for what I'm doing so hopefully this might help others

  • Related