You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"b.Write a program that asks user for three type of inputs,\n",
85
+
"a.print: if user enter print then it should print all countries with their population in this format,\n",
86
+
"\n",
87
+
"china==>143\n",
88
+
"\n",
89
+
"india==>136\n",
90
+
"\n",
91
+
"usa==>32\n",
92
+
"\n",
93
+
"pakistan==>21\n",
94
+
"\n",
95
+
"b.add: if user input add then it should further ask for a country name to add. If country already exist in our dataset then it should print that it exist and do nothing. If it doesn't then it asks for population and add that new country/population in our dictionary and print it\n",
96
+
"\n",
97
+
"c.remove: when user inputs remove it should ask for a country to remove. If country exist in our dictionary then remove it and print new dictionary using format shown above in (a). Else print that country doesn't exist!\n",
98
+
"\n",
99
+
"d.query: on this again ask user for which country he or she wants to query. When user inputs that country it will print population of that country."
100
+
],
101
+
"metadata": {
102
+
"id": "jESUFmKzg2ok"
103
+
}
104
+
},
105
+
{
106
+
"cell_type": "code",
107
+
"source": [
108
+
"population = {\n",
109
+
" 'china': 143,\n",
110
+
" 'india': 136,\n",
111
+
" 'usa': 32,\n",
112
+
" 'pakistan': 21\n",
113
+
"}\n",
114
+
"\n",
115
+
"def add():\n",
116
+
" country=input(\"Enter country name to add:\")\n",
117
+
" country=country.lower()\n",
118
+
" if country in population:\n",
119
+
" print(\"Country already exist in our dataset. Terminating\")\n",
120
+
" return\n",
121
+
" p=input(f\"Enter population for {country}\")\n",
122
+
" p=float(p)\n",
123
+
" population[country]=p # Adds new key value pair to dictionary\n",
124
+
" print_all()\n",
125
+
"\n",
126
+
"def remove():\n",
127
+
" country = input(\"Enter country name to remove:\")\n",
128
+
" country = country.lower()\n",
129
+
" if country not in population:\n",
130
+
" print(\"Country doesn't exist in our dataset. Terminating\")\n",
131
+
" return\n",
132
+
" del population[country]\n",
133
+
" print_all()\n",
134
+
"\n",
135
+
"def query():\n",
136
+
" country = input(\"Enter country name to query:\")\n",
137
+
" country = country.lower()\n",
138
+
" if country not in population:\n",
139
+
" print(\"Country doesn't exist in our dataset. Terminating\")\n",
140
+
" return\n",
141
+
" print(f\"Population of {country} is: {population[country]} crore\")\n",
142
+
"\n",
143
+
"def print_all():\n",
144
+
" for country, p in population.items():\n",
145
+
" print(f\"{country}==>{p}\")\n",
146
+
"\n",
147
+
"def main():\n",
148
+
" op=input(\"Enter operation (add, remove, query or print):\")\n",
"Enter operation (add, remove, query or print):add\n",
175
+
"Enter country name to add:england\n",
176
+
"Enter population for england345\n",
177
+
"china==>143\n",
178
+
"india==>136\n",
179
+
"usa==>32\n",
180
+
"pakistan==>21\n",
181
+
"england==>345.0\n"
182
+
]
183
+
}
184
+
]
185
+
},
186
+
{
187
+
"cell_type": "markdown",
188
+
"source": [
189
+
"**How it works:**\n",
190
+
"Uses a dictionary to store country => population.\n",
191
+
"\n",
192
+
"Based on user input:\n",
193
+
"\n",
194
+
"print → shows all countries and their populations.\n",
195
+
"\n",
196
+
"add → adds a new country if not already present.\n",
197
+
"\n",
198
+
"remove → removes a country if it exists.\n",
199
+
"\n",
200
+
"query → returns the population of the requested country.\n",
201
+
"\n",
202
+
"Handles case-insensitive inputs using .lower().\n",
203
+
"\n"
204
+
],
205
+
"metadata": {
206
+
"id": "C1Oygtg20YBS"
207
+
}
208
+
},
209
+
{
210
+
"cell_type": "markdown",
211
+
"source": [
212
+
"You are given following list of stocks and their prices in last 3 days,\n",
213
+
"\n",
214
+
"Stock\tPrices\n",
215
+
"\n",
216
+
"info\t[600,630,620]\n",
217
+
"\n",
218
+
"ril\t[1430,1490,1567]\n",
219
+
"\n",
220
+
"mtl\t[234,180,160]\n",
221
+
"\n",
222
+
"Write a program that asks user for operation. Value of operations could be,\n",
223
+
"\n",
224
+
"**print:** When user enters print it should print following,\n",
225
+
"\n",
226
+
"info ==> [600, 630, 620] ==> avg: 616.67\n",
227
+
"\n",
228
+
"ril ==> [1430, 1490, 1567] ==> avg: 1495.67\n",
229
+
"\n",
230
+
"mtl ==> [234, 180, 160] ==> avg: 191.33\n",
231
+
"\n",
232
+
"**add:** When user enters 'add', it asks for stock ticker and price. If stock already exist in your list (like info, ril etc) then it will append the price to the list. Otherwise it will create new entry in your dictionary. For example entering 'tata' and 560 will add tata ==> [560] to the dictionary of stocks."
"3.Write circle_calc() function that takes radius of a circle as an input from user and then it calculates and returns area, circumference and diameter. You should get these values in your main program by calling circle_calc function and then print them"
0 commit comments