The Farmer Was Replaced
My elif else dosn work (It wrong code spot)
I write my code the same way everyone but my elif and else dosnt work at all like error not valid at first it can be wrong but i search youtube as my is the same as them but it still dosnt work
Legutóbb szerkesztette: Poom36yu; 2024. máj. 23., 22:08
< >
1616/16 megjegyzés mutatása
Poom36yu eredeti hozzászólása:
I fonde out why it hapen look at this two
this one work
while True: if can_harvest(): harvest() else: move(North
this one doset
while True: if can_harvest(): harvest() else: move(North)

as real porgamer it a bit f normally the 2 code still work in progam

Very glad to hear it works! :steamthumbsup:

For a little more detail on why it might not have worked with the second example and in-case It might be a little helpful to anyone:
The main issue is that the Else statement has to match the indentation since the code in-game is heavily inspired by python or rather, is pretty much python with the exception of not requiring the players to import libraries and such.

So the first example you've wrote match properly with only the move(North which should have closed parentheses since it's a function and functions parentheses have to be closed to tell it that those are the arguments you're giving it.

And in the second example, as stated a little earlier in this post, the indentations have to match since in Python, you don't have the luxury of using braces to tell the compiler or interpreter that the method, loop or condition statement is enclosed at a specified line.
So to make it at least possible to have multiple conditions, the indentations are what's used to determine the end of a condition statement.
For example:
if Condition == Value: // Do something if AnotherCondition == AnotherValue: // Do something specific elif Condition == OtherValue: // Do another thing else: // Do something else

One thing that some people might also do, when the conditions are small enough to fit nicely on a single line, is combine them on the same if statement's condition depending on whether you need anything to happen between the two condition check or not.
For example:
if Condition == Value and AnotherCondition == AnotherValue: // Do something specific elif Statement == OtherValue: // Do another thing else: // Do something else

And another nice thing is like in your first example.
You can make your functions return True or False if you want such ability to determine if it worked when calling the function.
You can even have your if statement declare a variable directly within it by just writing one equal sign instead of two. (If I recall though you might need to then put that declaration into parentheses so that it's counted as one boolean comparison.

Alright, I'll stop there for the time being since I could probably easily keep going and most likely repeat the same thing a few more times instead of sleeping without noticing haha.
Glad it worked and hopefully any of this wall of text can be helpful to anyone! <3
Legutóbb szerkesztette: One More Quest; 2024. máj. 24., 23:09
< >
1616/16 megjegyzés mutatása
Laponként: 1530 50

Közzétéve: 2024. máj. 14., 8:10
Hozzászólások: 16