0

Trying to work with nested dictionaries.... Taking input from user about how many input items he has with him(he enters a no), for each item, the code asks him to enter two numbers, from which, the code shall tell the largest and lower number in the range of nos entered...

PFB Code:

def how_many_sets():
    global set_nos_list
    set_nos_list = []
    defaults = {"range": [], "items": [], "largest": 0, "lowest": 0, "difference": 0}
    global set_no
    set_no = int(input('How many sets?'))
    if set_no < 1 or set_no > 10:
        print('error')
    else:
        for num in range(set_no):
            set_nos_list.append(num)
        print(set_nos_list)
        global set_dict
        set_dict = dict.fromkeys(set_nos_list, defaults)
        return set_no


def take_sets():
    for item in set_nos_list:
        list_per_pair = list(map(int, input().split()))
        if len(list_per_pair) != 2:
            print('There is an error in the no of items, For finding the range two items should be there in a list')
            return
        elif (list_per_pair[0] < 2) or (list_per_pair[1] > 1000000) or (list_per_pair[0] > list_per_pair[1]):
            print(' error')
            return
        else:
            print(item)
            set_dict[item]["range"] = list_per_pair
            temp_item_list = []
            for value in range(list_per_pair[0], list_per_pair[1] + 1):
                # print(value)
                temp_item_list.append(value)
                print(temp_item_list)
            #set_dict[num]['items'] = temp_item_list

    print(set_dict)


def main():
    how_many_sets()
    take_sets()


# Write code here

main()

The line set_dict[item]["range"] = list_per_pair gives me an error, the set_dict gets updated with the last item value only despite the loop being iterating over all values....

How many sets?2
[0, 1]
3 6
0
[3, 4, 5, 6]
4 7
1
[4, 5, 6, 7]
{0: {'range': [3, 6], 'items': [], 'largest': 0, 'lowest': 0, 'difference': 0}, 1: {'range': [4, 7], 'items': [], 'largest': 0, 'lowest': 0, 'difference': 0}}

where as I want output something like this:

{0: {'range': [4, 7], 'items': [], 'largest': 0, 'lowest': 0, 'difference': 0}, 1: {'range': [4, 7], 'items': [], 'largest': 0, 'lowest': 0, 'difference': 0}}
phewww_
  • 13
  • 3

1 Answers1

0

From Initialize dict with values, you may change to something like

set_dict = {num: {**defaults} for num in range(set_no)}

Then avoid as possible global variables, prefer returning values frmo method and passing parameters

def how_many_sets():
    defaults = {"range": [], "items": [], "largest": 0, "lowest": 0, "difference": 0}
    set_no = int(input('How many sets? '))
    while set_no < 1 or set_no > 10:
        print('Amount set error')
        set_no = int(input('How many sets? '))
    return {num: {**defaults} for num in range(set_no)}


def take_sets(dict_structure):
    for item in dict_structure:
        list_per_pair = list(map(int, input("Gives values: ").split()))
        if len(list_per_pair) != 2:
            print('There is an error in the no of items, For finding the range two items should be there in a list')
            return
        elif (list_per_pair[0] < 2) or (list_per_pair[1] > 1000000) or (list_per_pair[0] > list_per_pair[1]):
            print('Bound Error')
            return
        else:
            dict_structure[item]["range"] = list_per_pair
            dict_structure[item]['items'] = list(range(list_per_pair[0], list_per_pair[1] + 1))

    print(dict_structure)


def main():
    dict_structure = how_many_sets()
    take_sets(dict_structure)


main()

Example

How many sets? 4
Gives values: 4 6
Gives values: 3 7
Gives values: 7 8
Gives values: 4 6

{0: {'range': [4, 6], 'items': [4, 5, 6], 'largest': 0, 'lowest': 0, 'difference': 0}, 
 1: {'range': [3, 7], 'items': [3, 4, 5, 6, 7], 'largest': 0, 'lowest': 0, 'difference': 0}, 
 2: {'range': [7, 8], 'items': [7, 8], 'largest': 0, 'lowest': 0, 'difference': 0}, 
 3: {'range': [4, 6], 'items': [4, 5, 6], 'largest': 0, 'lowest': 0, 'difference': 0}}
azro
  • 53,056
  • 7
  • 34
  • 70