Similar Posts

Subscribe
Notify of
22 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
PWolff
11 months ago

Do you know the meaning of indentations in Python? They seem to be a bit confused here.

PWolff
11 months ago
Reply to  Schlaumeier135

This should be almost at the very beginning in every Python course.

Maybe it will help you look at "Structograms" / "Nassi-Shneiderman diagrams" .

Break your code back into different levels – starting with

 if antwort == "links": elif antwort == "rechts": elif antwort == "vorne": elif antwort == "hinten": else:

Note that "if", the "elif"s and "else" are always exactly the same far indented

(I also put it in again because it looks like this should be in a loop – otherwise "Write again!" would make no sense.)

Then separately write the block to be executed at "left":

 while antwort not in Ja_Nein: # Stilfehler: # zwar ist in diesem konkreten Fall klar, # dass antwort beim ersten Mal niemals in Ja_Nein liegt, # aber dieser Codeblock ist nicht ohne Weiteres wiederverwendbar antwort = input() if antwort == "Ja": elif antwort == "Nein":

In order to be able to use this block within [if Response == "left":], you enter it further than [if Response == "left":]:

 while antwort not in Ja_Nein: # Stilfehler: # zwar ist in diesem konkreten Fall klar, # dass antwort beim ersten Mal niemals in Ja_Nein liegt, # aber dieser Codeblock ist nicht ohne Weiteres wiederverwendbar antwort = input() if antwort == "Ja": elif antwort == "Nein":

I would then proceed from the order in such a way that I then establish the framework for the cases "right", "front" and "backward".

For this block, next follows the block for "Yes" within the block for "left":

 chance = random.uniform(0, 1) if chance <= 0.6: else:

And again the indentations:

 chance = random.uniform(0, 1) if chance <= 0.6: else:

etc.

All that is below one line and is moved further is dependent on this line, up to the first following line, which is only as far as or less far engaged.

You will check in line 25 whether the answer == "left". Even if this is not the case, line 28 is executed – check whether answer == "Yes". Regardless of the answer to this, next check is whether chance <= 0.6.

The if-block of [chance <= 0.6] in line 30 is continued at [else] in line 34 – this is the first line after line 30, which is also far indented – and is formally continued in line 37 with the [elif] – that is also far indented.

But

 if else elif

is inadmissible, of course there is an error message. How should the interpreter/compiler know that this elif belongs to the if in line 28? Finally, it has been taught that indentations and ONLY give indentations, which belonged together and what is subordinate.

PWolff
11 months ago

Tip: take a text editor (or better a complete development environment) that “knows” the Python syntax. Then you will see at first glance what belongs together, and you can also “fold” whole blocks so that you can only see a coarser structure – this considerably promotes the overview.

At the very beginning, an advanced text editor like Notepad++ should be sufficient. Once it begins to become interesting, you will be grateful for an “integrated development environment” (IDE). On the Internet you can easily find a lot of discussions, which is now “best” and which is “most welcome” – you have to make yourself smart (and also decide whether you want to take a “lightweight” for the beginning and want to change later, or want to jump into the depths of thousands of settings at the beginning).

TUrabbIT
11 months ago

The problem seems to me that you’re trying an Elif on the same level after an Else. This is insane and invalid.

If I understand the code correctly, you need to enter the previous if else block with the chance that the compiler understands that your Elif refers to the If answer= yes.

Similar in the other code. You need to distinguish the blocks from each other by pressing.

Generally, the code seems very confusing to me. See if a match case setup is not better.

https://www.freecodecamp.org/news/python-switch-statement-switch-case-example/

smiregal8472
11 months ago

Simple: elif After other is a syntax error.

P.S.: However, there are many more errors in your code…

smiregal8472
11 months ago
Reply to  Schlaumeier135

Now you have a space in front of the elif, so it’s on the wrong level of engagement…

teehouse
11 months ago

After an Else, no Elif can follow. Doesn’t make any sense, as all the remaining cases have already been negotiated in the Else.

TheEmpire002
11 months ago

The error message means that your code does not correspond to the valid syntax.

You need to get the print statement right in line 38.

Besides, the whole if, elif, else Wirrwarr doesn’t make any sense.

TheLinuxer
11 months ago

I can see this a little bad now, but I would say that there are no spaces.

So it doesn’t:

elif antwort=="Nein":

It’s hot.

elif antwort == "Nein":

Otherwise I don’t see a mistake.

(In the code before, you had always set spaces and the code now works up to the point where you didn’t set)

TheLinuxer
11 months ago
Reply to  Schlaumeier135

Okay, sorry. But it was worth a try.

crimsonfire
11 months ago

so your problem has already been solved, but simply as a small tip:

please use blank characters everywhere the same, so not above zB x == y and below x=y that makes the code really unreadable

priesterlein
11 months ago

The meansthat you should first give UNBEDINGT the basics in logic and languages and learn how the syntax is correct in the new country. That the elif is not allowed to stand there and you have many other mistakes in it has already been written.

95Kickflip
11 months ago

I would ask Chat Gpt to copy the entire code directly and then the error message

95Kickflip
11 months ago
Reply to  95Kickflip

Isn’t the double point missing in line 43 and 46?