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



Download 2,02 Mb.
Pdf ko'rish
bet101/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   97   98   99   100   101   102   103   104   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Flag in a Program
In  our  last  example,  we  performed  a  task  pending  when  the  condition  is
true.  However,  what  if  it  is  a  more  complicated  program,  which  has
numerous events that can make the program stop?
For  instance,  a  game  can  have  several  exit  points.  Do  you  remember  the
famous  snake  game  in  Nokia  phones?  Remember  each  time  the  snake


touches the wall or touches itself, the game terminates. The game can also
terminate if you intentionally or mistakenly exit the game application. All
of these come with different possible events that might terminate the games.
Therefore, in testing these different conditions using a while loop statement
becomes difficult and complicated.
If  you  want  to  run  a  program  that  continues  running  pending  when  the
condition is false, then you have to define a variable, which will determine
if  the  program  is  active  or  not.  This  kind  of  variable  is  known  as  a  flag
because it acts as a pointer to the program. With this, we can create codes
that run while the variable “flag” is positioned to be True and stops when
any  of  the  different  event's  value  of  the  flag  is  False.  All  of  these  sound
complicated,  however,  let  us  upgrade  our  previous  example  by  using  the
flag in the program.
request = “\nSay something to me, and I will rephrase it perfectly:”
request += “\nIf you want to exit, Enter ‘stop’ to terminate the program!”
start = True
while start:
prompt1 = input(request)
if prompt1 == ‘stop’ :
start = False
else:
print(prompt1)
In  the  program  above,  we  set  “start”  to  be  “True,”  and  this  is  where  the
program  begins.  This  makes  the  while  loop  easier  because  there  is  no
comparison  tested  within  our  while  statement.  However,  the  logic  of  the
program is done in another part of the program. The loop will continue to
run pending when the expression becomes false.


The  if  a  statement  is  contained  within  the  while  loop,  which  checks  the
value of the prompt1 whenever the user enters their request. However, once
someone  enters  “stop,”  we  set  the  variable  “start”  to  False  thereby
triggering  the  loop  to  stop.  Notwithstanding,  anything  besides  the  quit
statement will print the request, which the user enters.
The program here will produce the same output as the previous one, where
we directly test the condition inside the while loop. However, with the flag,
we indicate when the program is an inactive state and makes it simpler to
include additional, conditional tests.



Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   97   98   99   100   101   102   103   104   ...   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