Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98115

Re: GoPiGo script

From Robin Koch <robin.koch@t-online.de>
Newsgroups comp.lang.python
Subject Re: GoPiGo script
Date 2015-11-02 20:31 +0100
Organization albasani.net
Message-ID <n18dmg$hvo$1@news.albasani.net> (permalink)
References <mailman.64.1446473424.4463.python-list@python.org> <tuKZx.1283$u_1.517@fx05.am1>

Show all headers | View raw


Am 02.11.2015 um 15:32 schrieb input/ldompeling@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

GoPiGo script input/ldompeling@casema.nl - 2015-10-30 16:58 +0000
  Re: GoPiGo script Peter Pearson <pkpearson@nowhere.invalid> - 2015-10-30 17:27 +0000
    Re: GoPiGo script input/ldompeling@casema.nl - 2015-11-02 13:29 +0000
      Re: GoPiGo script hakugin.gin@gmail.com - 2015-11-02 05:45 -0800
        Re: GoPiGo script hakugin.gin@gmail.com - 2015-11-02 05:49 -0800
          Re: GoPiGo script input/ldompeling@casema.nl - 2015-11-02 14:28 +0000
            Re: GoPiGo script hakugin.gin@gmail.com - 2015-11-02 07:19 -0800
            Re: GoPiGo script MRAB <python@mrabarnett.plus.com> - 2015-11-02 15:21 +0000
              Re: GoPiGo script hakugin.gin@gmail.com - 2015-11-02 07:30 -0800
      Re: GoPiGo script Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-02 09:10 -0500
        Re: GoPiGo script input/ldompeling@casema.nl - 2015-11-02 14:32 +0000
          Re: GoPiGo script Robin Koch <robin.koch@t-online.de> - 2015-11-02 20:31 +0100
            Re: GoPiGo script Larry Hudson <orgnut@yahoo.com> - 2015-11-02 13:06 -0800
              Re: GoPiGo script Robin Koch <robin.koch@t-online.de> - 2015-11-03 01:45 +0100
  Re: GoPiGo script hakugin.gin@gmail.com - 2015-10-30 10:32 -0700
    Re: GoPiGo script input/ldompeling@casema.nl - 2015-10-30 18:18 +0000
      Re: GoPiGo script hakugin.gin@gmail.com - 2015-10-30 12:59 -0700
        Re: GoPiGo script input/ldompeling@casema.nl - 2015-10-31 10:28 +0000
          Re: GoPiGo script MRAB <python@mrabarnett.plus.com> - 2015-10-31 17:42 +0000
            Re: GoPiGo script input/ldompeling@casema.nl - 2015-10-31 18:59 +0000
              Re: GoPiGo script MRAB <python@mrabarnett.plus.com> - 2015-10-31 19:42 +0000
                Re: GoPiGo script input/ldompeling@casema.nl - 2015-10-31 20:18 +0000
                Re: GoPiGo script MRAB <python@mrabarnett.plus.com> - 2015-10-31 21:12 +0000

csiph-web