+ 1

How to return dictionary?

The Human class must contain the following information: first name, last name, age, gender, nationality, and how to enter this information. h1 = Human(name="John", family name="Vik", age=35, gender="male", nationality="USA") List of methods: set_name() set_family() set_age() set_gender() set_nationality() get_info () # returns a dictionary with personal information about the object

2nd Feb 2021, 10:29 AM
Kuanysh Amangeldy
Kuanysh Amangeldy - avatar
4 Answers
+ 7
Kuanysh Amangeldy , i assume that you create instances from your class Human, and call the get_info() from the main program. assign this call as: my_result = x.get_info() # (x is the instance name) from get_info() you can use : my_dict {...} .... return my_dict this dict will then be in the var my_result, and can be used for whatever...
2nd Feb 2021, 11:35 AM
Lothar
Lothar - avatar
+ 4
Kuanysh Amangeldy , it would be helpful to see your code. also give a brief description what your code is going to do. thanks! here is general sample how to create a key:value pair in the dict. the variable names 'key' and 'value' can have any other name: my_dict = {} key = 5 value = 'ab-58' my_dict.update({key: value}) print(my_dict) #result: {5: 'ab-58'}
2nd Feb 2021, 5:59 PM
Lothar
Lothar - avatar
+ 1
Thank you so much ,Lothar !
2nd Feb 2021, 6:04 PM
Kuanysh Amangeldy
Kuanysh Amangeldy - avatar
0
how to add class values to the dictionary ?
2nd Feb 2021, 11:44 AM
Kuanysh Amangeldy
Kuanysh Amangeldy - avatar