Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python


Download 1.25 Mb.
Pdf ko'rish
bet19/48
Sana30.01.2023
Hajmi1.25 Mb.
#1140552
1   ...   15   16   17   18   19   20   21   22   ...   48
Bog'liq
Coding in Python A Comprehensive Beginners Guide to Learn the Realms

The if-else Statement
The condition can be interesting if you add to it the else statement to print a
statement if the count of items has not matured yet. The if-else statement
allows you to test the conditions in both ways. The else statement defines an
action when a particular condition fails.
officeitems = 11
if officeitems >= 10:
print("You can list the office for sale")
print("Have you listed it yet?")
else:
print("Sorry, you cannot list the office for sale.")
print("Please buy and add more items to the office and list it again for
sale.")
= RESTART: C:/Users/saifia computers/Desktop/sample.py
You can list the office for sale
Have you listed it yet?
>>>
As the items are more than 10, the condition has been tested passed. Now I
will reduce the count of the items to test the else-statement.


officeitems = 9
if officeitems >= 10:
print("You can list the office for sale")
print("Have you listed it yet?")
else:
print("Sorry, you cannot list the office for sale.")
print("Please buy and add more items to the office and list it again for
sale.")
= RESTART: C:/Users/saifia computers/Desktop/sample.py
Sorry, you cannot list the office for sale.
Please buy and add more items to the office and list it again for sale.
>>>
The if-elif-else Chain
The elif statement allows you to add one more condition to the block of code.
officeitems = 5
if officeitems < 10:
print("You still can list the office but it will not bring you the desired
amount of money.")
elif officeitems < 15:
print("You can list the office for sale")
print("Have you listed it yet?")
else:
print("Sorry, you cannot list the office for sale.")
print("Please buy and add more items to the office and list it again for
sale.")
= RESTART: C:/Users/saifia computers/Desktop/sample.py


You still can list the office, but it will not bring you the desired amount of
money.
>>>
You can test multiple conditions with conditional statements.
officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights']
if 'printer' in officeitems:
print("I have purchased the printer.")
if 'fan' in officeitems:
print("I have purchased the fan.")
if 'scanner' in officeitems:
print("I have purchased the scanner.")
= RESTART: C:/Users/saifia computers/Desktop/sample.py
I have purchased the printer.
I have purchased the fan.
I have purchased the scanner.
>>>
When the player has checked if he has purchased all the desired items, the
game will give him a green signal to list the office. You can add a print
statement at the end of the block of code.
officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights']
if 'printer' in officeitems:
print("I have purchased the printer.")
if 'fan' in officeitems:
print("I have purchased the fan.")
if 'scanner' in officeitems:
print("I have purchased the scanner.")
print("\nYou can list the office now.")


= RESTART: C:/Users/saifia computers/Desktop/sample.py
I have purchased the printer.
I have purchased the fan.
I have purchased the scanner.
You can list the office now.
>>>
The same code cannot work with an elif statement. It will stop working. See
what happens when we remove the simple if statement and add an elif
statement.
officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights']
if 'printer' in officeitems:
print("I have purchased the printer.")
elif 'fan' in officeitems:
print("I have purchased the fan.")
elif 'scanner' in officeitems:
print("I have purchased the scanner.")
print("\nYou can list the office now.")
= RESTART: C:/Users/saifia computers/Desktop/sample.py
I have purchased the printer.
You can list the office now.
>>>

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   15   16   17   18   19   20   21   22   ...   48




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