Objects
In this lesson: you will keep facts about one thing in one object, each under its own name.
Lesson 18
In this lesson: you will keep facts about one thing in one object, each under its own name.
In this lesson: you will keep facts about one thing in one object, each under its own name.
A list keeps values under numbers (indexes). An object keeps them under names:
1тағ хонанда = {2ном: "Зарина",3синф: 7,4аълочӣ: дуруст,5};6чоп(хонанда);7чоп(хонанда.ном);8чоп("Синф:", хонанда.синф);
{ }: name: value, separated by commas.name: value is a property (хосият). You read it with a dot: хонанда.ном.1тағ китоб = { ном: "Шоҳнома", саҳифаҳо: 500 };2китоб.саҳифаҳо = 520;3китоб.муаллиф = "Фирдавсӣ";4чоп(китоб);
1тағ хонандагон = [2{ ном: "Алӣ", хол: 78 },3{ ном: "Зарина", хол: 95 },4{ ном: "Фарҳод", хол: 88 },5];6тағ беҳтарин = хонандагон[0];7барои (тағ х аз хонандагон) {8агар (х.хол > беҳтарин.хол) {9беҳтарин = х;10}11}12чоп("Беҳтарин:", беҳтарин.ном, беҳтарин.хол);
хонанда.суроға gives беқимат.дарозӣ or илова: they have a special meaning.Read the name and the three marks of a student. Create the object { ном, холҳо } ("name", "marks") and print name: average, where the average is the average of the three marks.
Input: Four lines: the name, then three marks.
Output: name: average
Read the number of items н, then a name and a price for each item. Keep the items as objects in a list. Print the total cost and the name of the most expensive item.
Input: н, then two lines for each item: the name and the price.
Output: Ҳамагӣ: … ("Total") and Гаронтарин: … ("Most expensive")
Read the number of students н, then a name and a score for each of them. Print the name and the score of the best student (if there are several, the first of them).
Input: н, then two lines for each student: the name and the score.
Output: name score