Learning Python: The Ultimate Guide for Beginners to Coding with Python with Useful Tools


Using User Input to Fill a Dictionary



Download 2,02 Mb.
Pdf ko'rish
bet109/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   105   106   107   108   109   110   111   112   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Using User Input to Fill a Dictionary
You can request for input as much as required from a user to pass through a
while loop. The program below is a polling program whereby whenever we
pass  through  the  while  loop,  it  requests  input  from  the  user.  We  will  then
store  the  information  we  gather  from  the  users  in  a  dictionary.  The  idea
behind this is to enable us to link each response we get to a particular user’s
input.
remarks ={}
# let set a flag to indicate that the polling is active
poll_active = True
while poll_active
# request for the name of the user and their remark
fullname = input(“\Enter your full name:”)
remark = input(“What is your country of residence?”)
#store the remark in the dictionary.
remarks[fullname] = remark
#Let us know if someone is still available to take the poll.


next = input(“Is there anyone else available to answer a question? (yes/
no)”)
if next == “no”:
poll_active = False
# Our Poll is complete. Therefore, display the results.
Print(“\n**** Poll Results ****”)
for fullname, remark in remarks.item():
print (f “{fullname}, [remark] is my country of residence. “)
In  the  program  above,  we  defined  an  empty  dictionary  (remarks)  while
setting a flag to indicate when the poll is active. The program will continue
to run within the while loop as long as the poll_active is True.
Inside the while loop, the user is requested to enter their full name and their
current  country  of  residence.  The  information  inserted  by  the  user  is  then
stored  in  the  remarks  dictionary.  Additionally,  the  program  then  asks  the
user if they are any person available to take the poll. If the response is yes,
the programs go back to the while loop. However, if it is no, the poll-active
flag becomes false with the loop stops. The final line is the code that will
display the results of the program. let us run the program with the following
input:
Luis Gracious
Herbert Franklin
Lilian Cosmos
Enter your full name: Luis Gracious
What is your country of residence? South Africa
Is there anyone else available to answer a question? (yes/ no) yes


Enter your full name: Lilian Cosmos
What is your country of residence? Turkey
Is there anyone else available to answer a question? (yes/ no) yes
Enter your full name: Herbert Franklin
What is your country of residence? Canada
Is there anyone else available to answer a question? (yes/ no) no
**** Poll Results ****
Luis Gracious, South Africa is my country of residence.
Lilian Cosmos, Turkey is my country of residence.
Herbert Franklin, Canada is my country of residence.



Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   105   106   107   108   109   110   111   112   ...   126




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish