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
Multiple Lists
Up till now, we have worked with a single list. In this code sample, I will work on multiple lists. I will add a list of optional items that may or may not be purchased. However, purchasing these items will help increase the value of the office. officeitems = ['printer', 'scanner', 'fan', 'table', 'table lights'] optionalitems = ['pen', 'paper', 'drafting pads', 'books', 'water dispenser'] for optionalitem in optionalitems: if optionalitem in officeitems: print("The office has been set up will all the essential and optional items.") else: print("Sorry, I have not purchased the " + optionalitem + ".") print("\nBoss: You can list the office for sale only after you purchase the optional items and set them up in the office.") = RESTART: C:/Users/saifia computers/Desktop/sample.py Sorry, I have not purchased the pen. Sorry, I have not purchased the paper. Sorry, I have not purchased the drafting pads. Sorry, I have not purchased the books. Sorry, I have not purchased the water dispenser. Boss: You can list the office for sale only after purchasing the optional items and setting them up in the office. >>> I defined the list of office items and optional items. I created a loop through the optional items to check if they also are added to the office or not. Upon the checking of each item, a message is displayed. In the end, the boss gives his verdict on whether to list the office or not. In the code mentioned above, the office does not contain any item from the list of optional items, therefore it displayed the same message. You can change that by including one or two optional items in the list of office items. See the following code. officeitems = ['printer', 'paper', 'drafting pads', 'scanner', 'fan', 'table', 'table lights'] optionalitems = ['pen', 'paper', 'drafting pads', 'books', 'water dispenser'] for optionalitem in optionalitems: if optionalitem in officeitems: print("I have purchased the " + optionalitem + ".") else: print("Sorry, I have not purchased the " + optionalitem + ".") print("\nBoss: You can list the office for sale only after you purchase all the optional items and set them up in the office.") = RESTART: C:/Users/saifia computers/Desktop/sample.py Sorry, I have not purchased the pen. I have purchased the paper. I have purchased the drafting pads. Sorry, I have not purchased the books. Sorry, I have not purchased the water dispenser. Boss: You can list the office for sale only after purchasing all the optional items and setting them up in the office. >>> |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling