Answer to Question #277052 in Python for ayush

Question #277052

[M4_CSQ4] Draw a flowchart and construct a python program that accepts the items of tuples containing the category as the first entry and different components of the category along with their costs as the following entries. The job is to create a tuple that summarizes the costs for each category. [CO1] [L2]

Sample workout as given below:

Input:

2  //No of tuples

7  //No of items in 1st tuple

Education //Items in the 1st tuple Category

Primary  // Component

50  //Cost

Secondary

25

Higher

20

7 //No of items in 2nd tuple

Defense //Items in the 2nd tuple Category

Army //Component

25 //Cost

AirForce

40

Navy

45

Output:

((Education, 95), (Defense, 110))

Test Cases are

case=1

input =3

5

Stationary

marker

80

chalk

10

3

Communication

phone

250

5

Internet

Airtel

100

Jio

80

output=((Stationary, 90), (Communication, 250), (Internet, 180))



1
Expert's answer
2021-12-08T03:06:39-0500
# Python 3.9.5

def get_tuples():
    tuples = int(input('Enter number of tuples: '))
    i = 0
    list_tuples = []
    while i < tuples:
        list_item = []
        count = 0
        tuple_elem = create_tuple()
        for item in tuple_elem:
            try:
                count += int(item)
            except:
                continue
        tuple_cat = tuple_elem[0]
        list_item.append(tuple_cat)
        list_item.append(count)
        list_tuples.append(tuple(list_item))
        i += 1
    return tuple(list_tuples)

def create_tuple():
    list_len = int(input('Enter number items of tuple: '))
    i = 0
    list_elem = []
    while i < list_len:
        list_item = input('Enter tuple item: ')
        list_elem.append(list_item)
        i += 1
    tuple_elem = tuple(list_elem)
    return tuple_elem

def main():                                          # Call main function
    result = get_tuples()
    print(result)

if __name__ == '__main__':
    main()



The flowchart  is not possible to transfer because there is no file transfer function in the questions.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS