Algorithms For Dummies


Choosing between multiple options



Download 7,18 Mb.
Pdf ko'rish
bet174/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   170   171   172   173   174   175   176   177   ...   651
Bog'liq
Algorithms

Choosing between multiple options  

using nested decisions

Nesting is the process of placing a subordinate statement within another state-

ment. You can nest any statement within any other statement, in most cases. To 

see how this works, open a copy of IPython and type the following code:

def SecretNumber():

   One = int(input("Type a number between 1 and 10: "))

   Two = int(input("Type a number between 1 and 10: "))

  

   if (One >= 1) and (One <= 10):



      if (Two >= 1) and (Two <= 10):


CHAPTER 4

  Introducing Python for Algorithm Programming 

     83


         print('Your secret number is: ' 

+ str(One * Two))

      else:

         print("Incorrect second value!")

   else:

      print("Incorrect first value!")

In this case

SecretNumber()

 asks you to provide two inputs. Yes, you can get 

inputs from a user when needed by using the 

input()

 function. The 

int()

 func-


tion converts the inputs to a number.

There are two levels of 

if

 statement this time. The first level checks for the valid-



ity of the number in 

One


. The second level checks for the validity of the number in 

Two


. When both 

One


 and 

Two


 have values between 1 and 10, .

SecretNumber()

 out-

puts a secret number for the user.



To see 

SecretNumber()

 in action, type SecretNumber() and press Enter. Type 20 

and press Enter when asked for the first input value, and type 

10

 and press Enter 



when asked for the second. You see an error message telling you that the first 

value is incorrect. Type SecretNumber() and press Enter again. This time, use 

values of 10 and 20. The function will tell you that the second input is incorrect. 

Try the same sequence again using input values of 10 and 10.




Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   170   171   172   173   174   175   176   177   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
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