What They Never Told You About Programming - Part 3

Isabel is in a beginning programming class and has written the Python code below to try to print "Fruit Salad!" However, she's sad because her code below doesn't print out "Fruit Salad!". Which line in the code below needs to be corrected?

1
2
3
4
    apples = 10                 #line 1
    bananas = 10                #line 2
    if apples = bananas:        #line 3
         print "Fruit salad!"   #line 4

1 3 4 2

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

12 solutions

Discussions for this problem are now closed

Sharky Kesa
Apr 4, 2014

In line 3, there is only one equality sign instead of two. Hence, the code won't print out.

In line 4 its given "Fruit salad!" instead of "Fruit Salad!"

Zeeshan Khan S - 7 years, 1 month ago

I don't think that should be a problem because having a single '=' will assign the value and it will always be true. The mistake can be she is trying to compare two different types of variables which is not specified here. !!

dhruv Patel - 7 years, 2 months ago

On line 3 she used the assignment operator instead of the equality operator and that is why her code didn't print out "fruit salad"

Daniel Anyidoho - 7 years, 2 months ago

I agree with Sharky Kesa! There should indeed be another equality symbol.

Stupid Guy - 7 years, 2 months ago

@Stupid Guy You have a wierd screen name dude!

Ameya Salankar - 7 years, 2 months ago

Hahahaha good one Ameya Salankar.

Fuad Hasan Ifty - 7 years, 2 months ago

in line 3 there is only one equality sing instead of two. hence ,the code won' print out

Zain Khan - 7 years, 2 months ago

on line 3 ........ ; should be used at the end instead of :

Sumit Joshi - 7 years, 1 month ago

A single equality sign denotes assignment. To test for equality one needs to use two equality symbols, i.e. "if apples == bananas:"

Akshay Gangwar - 7 years, 1 month ago

On the third line if is used. "If" comes on a line there must be " then/GOTO "

So, there is a mistake on the third line.

(Python doesn't have a "then" or a "GOTO" afaik).

Aditya Rajendran - 7 years, 1 month ago
Raghav Dua
May 8, 2014

The answer is obviously 3. However, we must also take care that this kind of code will only (I believe) give unexpected output in python. Consider for example C or C++. the statement apple = banana will assign the value of banana to apple (10), so the condition essentially looks like:

if (10) print "Fruit Salad".

Since every non zero number in C and C++ represents a boolean true , the statement would then mean:

if (true) print "Fruit Salad"

which would indeed print that on the screen because condition is true, so the code that follows will be executed, contrary to this code in python.

Try running this in C++

//include iostream statement

using namespace std;

int main () {

int app = 10;

int ban = 10;

if (app = ban) {

cout << "Hello" << endl;

}

return 0;

}

Food for thought :-)

Ram Krishna
Apr 22, 2014

Line 3 is a assignment statement rather than a comparison statement which is "=="...

Pradeep Kumar
Apr 20, 2014

the correct line is if(apple==banana);

Sharif Chowdhury
Apr 18, 2014

it will be ==

Vaibhav Jain
Apr 17, 2014

in line 3 it would be '==' instead of '='

Payal Garg
Apr 12, 2014

if apples==bananas;

Asad Hussain
Apr 12, 2014

= sign is for assignment and == sign check equality and returns either true or false accordingly... so use == instead of = in line 3

Ayan Bhuyan
Apr 12, 2014

Error is in line 3 but it should still execute....

The "=" sign must be replaced by "=="

she had completed the line incorrectly by using colon and also used assignment operator ' = ' to compare the values.

Manmeswar Patnaik - 7 years, 2 months ago
Mahiuddin Rasel
Apr 9, 2014

Observe all of four lines!. Only ones difference is colon mark(:) and all are the line is ok from logical end. So the answer is 3

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...