GoPiGo script

Robin Koch robin.koch at t-online.de
Mon Nov 2 14:31:29 EST 2015


Am 02.11.2015 um 15:32 schrieb input/ldompeling at casema.nl:

> Thank you for the explanation for it.
> I must tell you that  i yust beginning with python.
> I bought the book beginning programming with python.

Have you programming experience with other languages?

Well, the very first thing you should have learned is the basic syntax 
of Python. (That's how a program has to be written to work properly.)

And one of those first things is that python code is structured not by 
braces (like many other programming languages), but by intendation (as 
mentioned by others before).

That means that everything "depending" of a line somehow has to be 
intended deeper than that line.

Those lines are typically lines starting with "if", "for", "while", 
"def" or other keywords and end with a colon.

In your code the ifs and fors (and the following lines) are correctly 
intended. But you are using the "while" and "def" kind of wrong.

Let me give an example (note that the line numbers are only for this 
posting to prevent funny formating business and to talk about the single 
lines!):

01 for i in [1,2,3]:
02     print('A')
03     print('B')
04 print('C')

The first line (01) starts a loop. (Starting with a for, ending with a 
colon.) For every entry in the given list "something" is supposed to be 
repeated. But what is repeated and what isn't?

Easy: All following *intended* lines (02-03) are repeated.
Line 04 isn't intended and marks the point where the loop ends.

The output of the above is:

A
B
A
B
A
B
C


If we change the code just a tiny bit:

01 for i in [1,2,3]:
02     print('A')
03 print('B')
04 print('C')

(Notice, that the only change is the removed whitespace in line 03.)

only line 02 is repeated:

A
A
A
B
C


That works also for if and while:

01 if False:
02     print("This isn't printed.")
03     print("This neigther.")
03 print("But this is!")

01 c = 1
02 while c < 10:
03     print(c)
04     c = c*2
05 print("done")

prints

1
2
4
8
done


And of course those are mixable:

01 for a in [3,5,7]:
02    b = 1
03    while a*b < 10:
04	if a*b % 2 != 0:
05            print(a*b)
06        b = b+1
07    print("Next a")
08 print("done")

prints:

3
9
Next a
5
Next a
7
Next a
done


And this is where your problems seems to be.

You start with

"while True:"

which does start an endless loop.

But: As explained it only repeats the lines theat follow and are intended!

In your original post the next line isn't intended at all, which is 
considered an error.
In your second attempt you intended only the first 5 lines. So those 
(and only those) are repeated.

The next line ("if mindist > us_dist(15):") wasn't intended and 
therefore *not* repeated.

If you just learning programming with Python you migth want to start 
with smaller examples to understand the syntax better.
Do you understand what the script is supposed to do in every line?

-- 
Robin Koch



More information about the Python-list mailing list