#You have a list fruits:
fruits=['orange', 'pineapple','banana','mango']
#Can you give me 2 things?
#1. a list called my_fruits
#my_fruits=['my_orange', 'my_pineapple','my_banana','my_mango']
#I saw The following solutions before:
my_fruits=[]
my_fruits += ['my'+fruits[0]]
my_fruits += ['my'+fruits[1]]
#I don't want you to do that.
#I want you to give me an answer using for loops.
fruits=['orange', 'pineapple','banana','mango']
my_fruits = []
for x in fruits:
my_fruits += ['my_' + x]
print my_fruits
#what does this print?
my_fruits = []
for x in fruits:
my_fruits += ['my_' + x]
print my_fruits
#what does this print?
#2. I would like you to give me a list called my_favorite_fruits
#with the elements pineapple and mango
#so my_favorite_fruits=['pinapple','mango']
my_favorite_fruits = []
for x in fruits:
y = [fruits[1]] + [fruits[3]] #['pinapple','mango']
my_favorite_fruits += y #['pinapple', 'mango']
if x==fruits[0]: #if x=='orange'
break
#my_favorite_fruits += [x]
print my_favorite_fruits
#3. give me a variable called best_fruit whose value is 'banana' and whose
#data type is a string:
#best_fruit='banana'
best_fruits =''
for x in fruits:
if x=='banana':
best_fruits = x
break
#print x
print best_fruits
#one of your classmates came up with this solution.
#what is wrong with this solution?
#print it and see what the output is.
fruits=['orange', 'pineapple','banana','mango']
my_favorite_fruits=[]
for x in fruits:
#x is banana
#y=['pinapple'+'mango]
y=[fruits[1]+fruits[3]]
my_favorite_fruits += y
print my_favorite_fruits
['pineapplemango', 'pineapplemango', 'pineapplemango', 'pineapplemango']
#What is printed?
fruits=['orange', 'pineapple','banana','mango']
for x in fruits:
if x=='pineapple':
print x
continue
#Can you give me a function called reverse, that takes in an input string
#And returns the reversed version of the string.
#For example, if the input is abcde it returns edcba.
#This will give you an exercise using len and range
#One solution that one of your classmates came up with
def reverse(input_string):
z=0
n=''
for x in input_string:
y=input_string[len(input_string)-1-z] #z=0
z += 1
n += y
return n
#Another solution
def reverse(x):
y=''
for i in range(len(x)):
y += x[len(x)-i-1]
return y
d=reverse('abcdeghefggig')
print d
giggfehgedcba
#What is the difference between these 2 solutions?
#Which solution is better?