Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
If Statements and Lists
You can pair up if statements with lists. You can actually combine the two and do some amazing things. In the next example, I will pair up a loop with a list to make the game more interactive. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] for officeitem in officeitems: print("I have purchased the " + officeitem + ".") print("\nBoss: You can list the office for sale now.") = RESTART: C:/Users/saifia computers/Desktop/sample.py I have purchased the printer. I have purchased the scanner. I have purchased the fan. I have purchased the table. I have purchased the table lights. Boss: You can list the office for sale now. >>> You have a straightforward result because the code contains a simple for loop. However, you can add a bit more complexity to the code. The boss may ask the employee about a certain item that must be included in the office set up before the office is put on sale. The employee has to handle the situation in the most appropriate way. I will add an if-else statement along with the for loop to make the code more flexible. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] for officeitem in officeitems: if officeitem == 'cupboard': print("Sorry Boss, I have not purchased it yet.") else: print("However, I have purchased the " + officeitem + ".") print("\nBoss: You can list the office for sale only after you purchase the cupboard and set it up in the office.") = RESTART: C:/Users/saifia computers/Desktop/sample.py However, I have purchased the printer. However, I have purchased the scanner. However, I have purchased the fan. However, I have purchased the table. However, I have purchased the table lights. Boss: You can list the office for sale only after purchasing the cupboard and setting it up in the office. >>> Now, the Python interpreter checks each office item before it displays the message. The code confirms if the boss has asked for the cupboard. When he asks for the item, he gets a different response from the employee. The else block makes sure that the response is in affirmative for all the other items. Download 1.25 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling