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 |
|
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.
In line 4 its given "Fruit salad!" instead of "Fruit Salad!"
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. !!
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"
I agree with Sharky Kesa! There should indeed be another equality symbol.
@Stupid Guy You have a wierd screen name dude!
Hahahaha good one Ameya Salankar.
in line 3 there is only one equality sing instead of two. hence ,the code won' print out
on line 3 ........ ; should be used at the end instead of :
A single equality sign denotes assignment. To test for equality one needs to use two equality symbols, i.e. "if apples == bananas:"
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).
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 :-)
Line 3 is a assignment statement rather than a comparison statement which is "=="...
the correct line is if(apple==banana);
in line 3 it would be '==' instead of '='
= sign is for assignment and == sign check equality and returns either true or false accordingly... so use == instead of = in line 3
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.
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
Problem Loading...
Note Loading...
Set Loading...
In line 3, there is only one equality sign instead of two. Hence, the code won't print out.