|
| 1 | +print ' ' |
| 2 | +print '# Create a list that contains Apples, Pears, Oranges and Peaches and display the list.' |
| 3 | +fruits = ['Apples', 'Pears', 'Oranges', 'Peaches'] |
| 4 | +print fruits |
| 5 | +print ' ' |
| 6 | + |
| 7 | +print '# Ask the user for another fruit and add it to the end of the list and print the list.' |
| 8 | +fruit1 = raw_input('Enter another fruit: ') |
| 9 | +fruits.append(fruit1) |
| 10 | +print fruits |
| 11 | +print ' ' |
| 12 | + |
| 13 | +print '# Ask the user for a number and display the number back to the user and the fruit' |
| 14 | +print 'corresponding to that number (on a 1-is-first basis).' |
| 15 | +i = int(raw_input('Enter a number between 1 and 5: ')) |
| 16 | +print i, fruits[i-1] |
| 17 | +print ' ' |
| 18 | + |
| 19 | +print '# Add another fruit to hte beginning of the list using "+" and display the list.' |
| 20 | +fruits = ['Bananas'] + fruits |
| 21 | +print fruits |
| 22 | +print ' ' |
| 23 | + |
| 24 | +print '# Add another fruit to the beginning of the list using insert() and display the list.' |
| 25 | +fruits.insert(0,'Grapes') |
| 26 | +print fruits |
| 27 | +print ' ' |
| 28 | + |
| 29 | +print '# Display all the fruits that begin with "P", using a for loop.' |
| 30 | +for fruit in fruits: |
| 31 | + if fruit[0] == 'P': |
| 32 | + print fruit |
| 33 | + |
| 34 | +print ' ' |
| 35 | +print '# Display the list.' |
| 36 | +print fruits |
| 37 | +print ' ' |
| 38 | + |
| 39 | +print '# Remove the last fruit from the list and display the list.' |
| 40 | +fruits.pop() |
| 41 | +print fruits |
| 42 | +print ' ' |
| 43 | + |
| 44 | +print '# Double the list.' |
| 45 | +print '# Ask the user for a fruit to delete and find it and delete it.' |
| 46 | +print '# Keep asking until they enter a fruit that is in the list.' |
| 47 | +print '# Remove all occurrences of the fruit and display the list.' |
| 48 | +fruits *= 2 |
| 49 | +x = raw_input('Please enter a fruit to delete: ') |
| 50 | +while x not in fruits: |
| 51 | + x = raw_input('That fruit is not in the list. Please try again: ') |
| 52 | +while x in fruits: |
| 53 | + fruits.remove(x) |
| 54 | +print fruits |
| 55 | +print ' ' |
| 56 | + |
| 57 | + |
| 58 | +print '# Ask the user for input displaying a line like Do you like apples?' |
| 59 | +print '# for each fruit in the list (making the fruit all lowercase).' |
| 60 | +print '# For each no, delete that fruit from the list.' |
| 61 | +print '# For any answer that is not yes or no, prompt the user to answer with one of those two values (a while loop is good here):' |
| 62 | +print '# Display the list.' |
| 63 | +fruits = ['Apples', 'Pears', 'Oranges', 'Peaches'] |
| 64 | +for fruit in fruits[:]: |
| 65 | + x = raw_input('Do you like ' + fruit.lower() + '?') |
| 66 | + while x not in ['yes', 'no']: |
| 67 | + x = raw_input('Please answer yes or no. Do you like ' + fruit.lower() + '?') |
| 68 | + if x == 'no': |
| 69 | + fruits.remove(fruit) |
| 70 | +print fruits |
| 71 | +print '' |
| 72 | + |
| 73 | + |
| 74 | +print '# Once more, using the list from series 1:' |
| 75 | +print '# Make a copy of the list and reverse the letters in each fruit in the copy.' |
| 76 | +print '# Delete the last item of the original list. Display the original list and the copy.' |
| 77 | +fruits = ['Apples', 'Pears', 'Oranges', 'Peaches'] |
| 78 | +new_fruits = fruits[:] |
| 79 | +for i, fruit in enumerate(new_fruits): |
| 80 | + new_fruits[i] = fruit[::-1] |
| 81 | +fruits.pop() |
| 82 | +print 'Original', fruits |
| 83 | +print 'Copy', new_fruits |
| 84 | + |
0 commit comments