|
| 1 | +# list lab execises from session-03 |
| 2 | + |
| 3 | +import random |
| 4 | +import string |
| 5 | +import sys |
| 6 | +import os |
| 7 | + |
| 8 | +# List of fruits |
| 9 | +mylist = ["apples", "pears","oranges","peaches"] |
| 10 | + |
| 11 | +# Display the list |
| 12 | +print('\n',"List of fruit",mylist,'\n') |
| 13 | + |
| 14 | +# Prompt for another type of fruit |
| 15 | +newfruit = input('Enter a type of fruit:') |
| 16 | + |
| 17 | +mylist.append(newfruit) |
| 18 | + |
| 19 | +mylen = len(mylist) |
| 20 | + |
| 21 | +# Display the list with the new fruit added |
| 22 | +print("List of fruit with the {} added".format(newfruit),mylist,'\n') |
| 23 | + |
| 24 | +# Prompt for a number |
| 25 | +num = int(input('Enter a number from 1 to {:d}:'.format(mylen))) |
| 26 | +#int(input("Please enter an integer: ")) |
| 27 | + |
| 28 | +for i in range(0,mylen): |
| 29 | + if (i == num-1): |
| 30 | + print("The fruit at that number is",mylist[i],'\n') |
| 31 | + |
| 32 | +addfruit = ["lemon"] |
| 33 | +mylist = addfruit + mylist |
| 34 | + |
| 35 | +# Display the list with lemon added |
| 36 | +print("List of fruit with lemon added",mylist) |
| 37 | + |
| 38 | +mylist.insert(0,"kiwi") |
| 39 | + |
| 40 | +# Display the list with kiwi added |
| 41 | +print("List of fruit with kiwi added",mylist,'\n') |
| 42 | + |
| 43 | +for i in mylist: |
| 44 | + # find fruits that start with a 'P' |
| 45 | + if (i[0] == "p"): |
| 46 | + print("This fruit starts with p",i,'\n') |
| 47 | + |
| 48 | + |
| 49 | +############################## Series 2 ---- actions ####################################### |
| 50 | +# Display the list |
| 51 | +print("List of fruit:",mylist,'\n') |
| 52 | + |
| 53 | +# Remove the last fruit |
| 54 | +mylist.pop() |
| 55 | + |
| 56 | +# Display the list |
| 57 | +print("List of fruit with the last item removed:",mylist,'\n') |
| 58 | + |
| 59 | +# Double the elements in the list |
| 60 | +mylist = mylist + mylist |
| 61 | +# Display the list |
| 62 | +print("List of fruit.... multiplied by 2:",mylist,'\n') |
| 63 | + |
| 64 | +# Prompt for a fruit to delete |
| 65 | +delfruit = input('Enter a fruit to be deleted:') |
| 66 | + |
| 67 | +temp = mylist.copy() |
| 68 | +lenth = len(temp) |
| 69 | + |
| 70 | +# for loop to go through the list and delete occurances of the seleted fruit |
| 71 | +for i in range(0,(lenth-1)): |
| 72 | + if (temp[i] == delfruit): |
| 73 | + #delete the spcified fruit |
| 74 | + mylist.remove(temp[i]) |
| 75 | + else: |
| 76 | + print("This fruit was not selected for removal",temp[i],'\n') |
| 77 | + |
| 78 | +# Display the list with the selected fruit removed |
| 79 | +print("List of fruit with all occurances of {} removed".format(delfruit),mylist,'\n') |
| 80 | + |
| 81 | +############################## Series 3 ---- actions ####################################### |
| 82 | + |
| 83 | +# Change each fruit's first letter to capital |
| 84 | +ntemp = mylist.copy() |
| 85 | +for i, elem in enumerate(ntemp): |
| 86 | + s = elem |
| 87 | + mylist.remove(elem) |
| 88 | + # Set the first letter in each item in the list to upper case |
| 89 | + s = s.capitalize() |
| 90 | + elem = s |
| 91 | + # insert the the capitalized item right back in its place |
| 92 | + mylist.insert(i,elem) |
| 93 | + |
| 94 | +# Display the list with each item capitalized |
| 95 | +print("List of fruit with each item capitalized ...:",mylist,'\n') |
| 96 | + |
| 97 | +# Change each fruit's first letter to lower case |
| 98 | +ntemp = mylist.copy() |
| 99 | +for i, elem in enumerate(ntemp): |
| 100 | + s = elem |
| 101 | + mylist.remove(elem) |
| 102 | + # Set the first letter in each item in the list to lower case |
| 103 | + s = s.lower() |
| 104 | + elem = s |
| 105 | + # insert the the capitalized item right back in its place |
| 106 | + mylist.insert(i,elem) |
| 107 | + |
| 108 | +# Display the list with each item in lower case |
| 109 | +print("List of fruit with each item in lower case ...:",mylist,'\n') |
| 110 | + |
| 111 | +tlist = mylist.copy() |
| 112 | +# Loop through the list and ask the user if they like the fruit item |
| 113 | +for i in tlist: |
| 114 | + # Prompt for a fruit to delete |
| 115 | + answ = (input('Do you like {}:'.format(i))) |
| 116 | + if (answ == "no"): |
| 117 | + # Remove the item |
| 118 | + mylist.remove(i) |
| 119 | + elif (answ == "yes"): |
| 120 | + print('you like this fruit:',i,'\n') |
| 121 | + # insist on a "yes" or "no" answer |
| 122 | + else: |
| 123 | + answ = input('Enter "yes" or "no" Please:') |
| 124 | + if (answ == "no"): |
| 125 | + # Remove the item |
| 126 | + mylist.remove(i) |
| 127 | + elif (answ == "yes"): |
| 128 | + print('you like this fruit:',i,'\n') |
| 129 | + |
| 130 | +# Display the list of liked fruits |
| 131 | +print("List of fruit with the ones you don't like removed:",mylist,'\n') |
| 132 | + |
| 133 | +############################## Series 4 ---- actions ####################################### |
| 134 | + |
| 135 | +# copy the last list |
| 136 | +nextlist = mylist.copy() |
| 137 | +print("List of fruit displayed... ",nextlist,'\n') |
| 138 | +nlist = [] |
| 139 | +ilist = [] |
| 140 | +# loop through the list of fruit items |
| 141 | +for i in nextlist: |
| 142 | + n = 1 |
| 143 | + strg = '' |
| 144 | + # Loop through each fruit item to get each letter that needs to be reversed |
| 145 | + for j in i: |
| 146 | + ln = len(i) |
| 147 | + last = (i[ln-n]) |
| 148 | + n = n+1 |
| 149 | + if (n > 1): |
| 150 | + strg = ''.join((strg,last)) |
| 151 | + ilist = [strg] |
| 152 | + nlist = nlist + ilist |
| 153 | + |
| 154 | +# Display the list of liked fruits |
| 155 | +print("List of fruit with letters in each fruit reversed...",nlist,'\n') |
| 156 | + |
| 157 | +# copy the list |
| 158 | +lastlist = nextlist.copy() |
| 159 | + |
| 160 | +# Remove last item in the list |
| 161 | +lastlist.pop() |
| 162 | + |
| 163 | +# Display the original list |
| 164 | +print("The original list of fruit displayed... ",nextlist,'\n') |
| 165 | + |
| 166 | + |
| 167 | +# Display the original list |
| 168 | +print("The original list of fruit with the last item deleted... ",lastlist,'\n') |
0 commit comments