+ 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
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...
+ 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'}
+ 1
Thank you so much ,Lothar !
0
how to add class values to the dictionary ?