18change it up


#1



I don't really understand the first code meaning.
Why there are no other fruits in the output? Thank you.
#output
You have...
A tomato is not a fruit!
A grape
A fine selection of fruits!
None


###
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
print 'You have...'
for f in fruits:
    if f == 'tomato':
        print 'A tomato is not a fruit!' # (It actually is.)    
  
else:   
    print 'A', f
    print 'A fine selection of fruits!'

#output
"""You have...
A tomato is not a fruit!
A grape
A fine selection of fruits!
None"""

###
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']

print 'You have...'
for f in fruits:
    if f == 'tomato':
        print 'A tomato is not a fruit!' # (It actually is.)    
  
    else:   
        print 'A', f
        print 'A fine selection of fruits!'
#output
"""You have...
A banana
A fine selection of fruits!
A apple
A fine selection of fruits!
A orange
A fine selection of fruits!
A tomato is not a fruit!
A pear
A fine selection of fruits!
A grape
A fine selection of fruits!
None"""


#2

Do you mean for this code?

print 'You have...'
for f in fruits:
    if f == 'tomato':
        print 'A tomato is not a fruit!' # (It actually is.)    
  
else:   
    print 'A', f
    print 'A fine selection of fruits!'

It doesn't define fruits, the output for just this code isn't what you describe. You must have run some other code earlier which created that variable


#3

I forgot to paste the separate code here, I had it when I run the code.
I mean why the output didn't show other fruits but only grape?


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.