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
Del Method
There may be occasions in the game when a player wants to remove certain items from the office to adjust the office to his desires. You can easily remove different items from a list by a simple method. The first method is that of the del statement. Let us check how you can add this method to your code to make the game more interactive. officeitems = ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] print(officeitems) del officeitems[0] print(officeitems) del officeitems[1] print(officeitems) del officeitems[2] print(officeitems) = RESTART: C:/Users/saifia computers/Desktop/sample.py ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] ['scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] ['scanner', 'table', 'chair', 'computer system', 'table lights'] ['scanner', 'table', 'computer system', 'table lights'] >>> Pop Method While the del method demands from you that you mention the index number, you always have the choice to use another method, the pop() method, to remove items from your lists. This is helpful if you want to give your players the freedom to randomly remove items from the office setup. The pop() method tends to eject items from the end of the list. However, you can use the item later on. This is why the pop() method is different from the del() method. Imagine that the office items are stacked up one upon another and you get the opportunity to pick and throw out from the top one by one. This is how the pop() method works. I will use the pop() method until the list stands empty to show how a player can empty an office off the items if he wants to. officeitems = ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] print(officeitems) popped_officeitems = officeitems.pop() print(officeitems) print(popped_officeitems) popped_officeitems = officeitems.pop() print(officeitems) print(popped_officeitems) popped_officeitems = officeitems.pop() print(officeitems) print(popped_officeitems) popped_officeitems = officeitems.pop() print(officeitems) print(popped_officeitems) popped_officeitems = officeitems.pop() print(officeitems) print(popped_officeitems) popped_officeitems = officeitems.pop() print(officeitems) print(popped_officeitems) popped_officeitems = officeitems.pop() print(officeitems) print(popped_officeitems) = RESTART: C:/Users/saifia computers/Desktop/sample.py ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system'] table lights ['printer', 'scanner', 'fan', 'table', 'chair'] computer system ['printer', 'scanner', 'fan', 'table'] chair ['printer', 'scanner', 'fan'] table ['printer', 'scanner'] fan ['printer'] scanner [] printer >>> Once there are no more items in the list but you still use the pop() method, it will return an error that will look like the following. Traceback (most recent call last): File "C:/Users/saifia computers/Desktop/sample.py", line 24, in popped_officeitems = officeitems.pop() IndexError: pop from empty list In the following section, I will show you how you can use the popped item in the code. If you want your player to explain to his boss why he removed an item from the item, you can add another line of code to your program. officeitems = ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] print(officeitems) popped_officeitems = officeitems.pop() print("I have sold " + popped_officeitems.title() + " because it was not adding the desired value to the office.") = RESTART: C:/Users/saifia computers/Desktop/sample.py ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] I have sold Table Lights because it was not adding the desired value to the office. >>> Unlike what most people think, the pop() method allows you to remove items from a list at specific positions of your choice. You have to fill in the parenthesis with the desired index number. officeitems = ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] print(officeitems) popped_officeitems = officeitems.pop(3) print("I have sold the " + popped_officeitems.title() + " because it was not adding the desired value to the office.") = RESTART: C:/Users/saifia computers/Desktop/sample.py ['printer', 'scanner', 'fan', 'table', 'chair', 'computer system', 'table lights'] I have sold the Table because it was not adding the desired value to the office. >>> 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