Why am I getting this error in python when i enter into next line of if-else condition? - python-3.7

if cake == "delicious":
return "yes"
SyntaxError: 'return' outside function
Why I get like this?

You can create some functions like this.
text ="delicious"
def check(text):
if text=="delicious":
return 'yes'
check(text)

return is a keyword which can only appear in a function definition (as the syntax error says). You can simply print your output in an if/else block
if cake == 'delicious': print('yes')

Related

Which if else code should I use, single line or multiple lines?

There are two types of if function codes:
condition1 ? function1 : condition2 ? function2 : function3;
if (condition1) {
function1
} else {
if (condition2) {
function2
} else {
function3
}
}
I don't know what is the correct way to use the if else function code, I hope everyone can tell me.
Feel free to leave a comment if you need more information.
Which if else code should I use, single line or multiple lines? I would appreciate any help. Thank you in advance!
Those two are not "different styles of if".
The first one is the ternary operator. As the term operator suggests, it is used to produce a value.
The second one is the flow control statement if. It does not produce any value, it changes your program's flow.
So which one should you use? The one that fits your goal best. Do you need a value? The operator. Do you need to change your program flow? The flow control statement.
Try to reproduce this if statement with a ternary operator:
if (trafficlight.current == red) {
stopVehicle();
}
You cannot. Not without adding pointless waste. Because this is flow control.
On the other hand, this:
var newSpeed = (trafficlight.current == red) ? 0 : this.MaxSpeed;
Would be very convoluted to write as an if statement. Because it is generating a value.
So pick what is best for your program. It's not a "style" to follow blindly. It is a decision you should make for every one of those instances.

How to call functions inside ternary operators in UnityScript?

I'm trying to decide which function to call, based on a boolean value.
myBooleanVariable ? function1() : function2();
Unity gives the error :
Expressions in statements must only be executed for their
side-effects.
So why does this not work, and how can I make it work ?
Thanks for any help !
So why does this not work, and how can I make it work ?
If it's true that it doesn't work (I don't have Unity to hand), it means UnityScript (Unity's implementation of JavaScript) doesn't support the expression statement. Which puts it at variance with the specification, and means a fair number of JavaScript idioms won't work in it. Your line is perfectly valid JavaScript/ECMAScript. You might check to see if there are "lint"-style options you can enable/disable.
The solution would be to use the result of the expression, or rewrite that using if.
Use the result:
var f = myBooleanVariable ? function1() : function2();
Using if:
if (myBooleanVariable) {
function1();
}
else {
function2();
}
Or if you really want the if to be on one line:
if (myBooleanVariable) function1(); else function2();

Error when using conditional breakpoint with instanceof.Is it me or eclipse?

My condition for break :
event instanceof org.geomajas.gwt.client.widget.event.SearchEvent
I have tried other variations like event instanceof SearchEvent / with parantheses and with/out ";"
The error : Evaluations must contain either an expression or a block of well-formed statements
The solution: ?
BTW I'm using jdk 1.6.25
return event instanceof org.geomajas.gwt.client.widget.event.SearchEvent;
should do the trick

How do you write an empty while loop in Coffeescript?

I'm trying to translate some old code to Coffeescript. But there is no direct translation for:
while ( doWork() ) {}
"while doWork()" with nothing after it results in a syntax error.
while doWork() then
Should do the trick
using then is probably the canonical solution since it is explicitly meant for separating the condition from the (in this case empty) body. Alternatively you can write
while doWork()
;#
(the # keeps vim syntax highlighting from flagging it as an error)
I also like the continue while doWork() solution, but I strongly advise against any other form of expression while doWork() mentioned in the comments since when this is the last statement of a function it will become a list constructor:
_results = [];
while (doWork()) {
_results.push(expression);
}
return _results;

; expected but <place your favourite keyword here> found

I'm trying to write a class for a scala project and I get this error in multiple places with keywords such as class, def, while.
It happens in places like this:
var continue = true
while (continue) {
[..]
}
And I'm sure the error is not there since when I isolate that code in another class it doesn't give me any error.
Could you please give me a rule of thumb for such errors? Where should I find them? are there some common syntactic errors elsewhere when this happens?
It sounds like you're using reserved keywords as variable names. "Continue", for instance, is a Java keyword.
You probably don't have parentheses or braces matched somewhere, and the compiler can't tell until it hits a structure that looks like the one you showed.
The other possibility is that Scala sometimes has trouble distinguishing between the end of a statement with a new one on the next line, and a multi-line statement. In that case, just drop the ; at the end of the first line and see if the compiler's happy. (This doesn't seem like it fits your case, as Scala should be able to tell that nothing should come after true, and that you're done assigning a variable.)
Can you let us know what this code is inside? Scala expects "expressions" i.e. things that resolve to a particular value/type. In the case of "var continue = true", this does not evaluate to a value, so it cannot be at the end of an expression (i.e. inside an if-expression or match-expression or function block).
i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
}
This is a problem, as the function block is an expression and needs to have an (ignored?) return value, i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
()
}
() => a value representing the "Unit" type.
I get this error when I forget to put an = sign after a function definition:
def function(val: String):Boolean {
// Some stuff
}