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



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

Giving the Users a Choice
We can write a program that runs continuously, depending on the choice of
the  user.  We  can  do  this  by  putting  the  program  inside  the  while  loop.


However, we will define an exit value that will quit the program. if the user
doesn’t input that number, the program will continue to run. Consider the
program below:
request = “\nSay something to me, and I will rephrase it perfectly:”
request += “\nIf you want to exit, Enter ‘stop’ to terminate the program!”
prompt1 = “”
while prompt1 != ‘stop’ :
prompt1 = input(request)
print(prompt1)
In the first line, we define a variable request and give them two options on
what to do. The first is to enter a message, whereas the second is to exit the
program  by  entering  stop.  Them  in  line  three,  we  defined  any  variable
“prompt” with an empty string. During the first time, the interpreter checks
on the variable and compare the value of the prompt1 to see if the user has
input  “stop.”  However,  the  user  hasn’t  inputted  any  value  yet.  The  while
loop will continue to run as long as the prompt isn’t equal to ‘stop.’
During  the  first  loop,  a  prompt1  is  just  a  string  without  any  value,  so  the
loop  begins.  Then  at  prompt1  =  input(request),  it  shows  the  request  and
waits on the user to respond. Whatever message the user enter is printed out
on the screen, and the interpreter reevaluates the condition to ascertain that
the condition is still within the while loop statement. Since the user hasn’t
entered the word ‘quit,’ the program further asks the user to make an input.
However, once the user enters “stop,” the program stops executing the loop
and halts the program.
Let  assume  the  users  in  his  interaction  with  the  program  entered  the
following statements.
I am new to Python Programming!
Programming in Python is the best for beginners!
I am in an advanced learning phase in Python Programming!
My Python course will soon end!


stop
Then the output will be as follows:
Say something to me, and I will rephrase it perfectly:
If you want to exit, Enter ‘quit’ to terminate the program!
I am new to Python Programming
Say something to me, and I will rephrase it perfectly:
If you want to exit, Enter ‘quit’ to terminate the program!
Programming in Python is the best for beginners!
Say something to me, and I will rephrase it perfectly:
If you want to exit, Enter ‘quit’ to terminate the program!
I am in an advanced learning phase I Python in Python Programming!
Say something to me, and I will rephrase it perfectly:
If you want to exit, Enter ‘quit’ to terminate the program!
stop
If you observe, the program included the “quit” word. Naturally, we don’t
want that word printed out on the screen. Well, to avoid such a situation, we
can use the simple if statement to fix it.
request = “\nSay something to me, and I will rephrase it perfectly:”
request += “\nIf you want to exit, Enter ‘stop’ to terminate the program!”
prompt1 = “”
while prompt1 != ‘stop’ :


prompt1 = input(request)
if prompt1 != ‘stop’:
print(prompt1)
Now  reenter  the  statements  you  entered  the  first  time  and  observed  the
difference. If properly done, your output will be as follow
Say something to me, and I will rephrase it perfectly:
If you want to exit, Enter ‘stop’ to terminate the program!
I am new to Python Programming
Say something to me, and I will rephrase it perfectly:
If you want to exit, Enter ‘stop’ to terminate the program!
Programming in Python is the best for beginners!
Say something to me, and I will rephrase it perfectly:
If you want to exit, Enter ‘stop’ to terminate the program!
I am in an advanced learning phase I Python in Python Programming!
Say something to me, and I will rephrase it perfectly:
If you want to exit, Enter ‘stop’ to terminate the program!

Download 2,02 Mb.

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