Page 1 of 2 12 LastLast
Results 1 to 10 of 16
  1. #1
    Mike Mike's Avatar
    Join Date
    Dec 2004
    Location
    Rosarito, Mexico
    Posts
    1,382

    Default Looping in Python

    I'm hoping there is someone here who knows a little more Python than I do

    I have created a script that repeats itself, and I want to put a loop in so that I don't have the script repeated 13 times

    The only difference each time the script runs is a couple of variables (which could possibly be reduced to one)

    I know you can use something like the "for" command, but I'm not sure quite how to use it (if that is the right command?)

    Anyone able to help? I know I haven't explained myself very well, but perhaps you could ask the right questions so I know where I can explain it better

    Cheers,
    Mike.
    what are you doing looking in here?

  2. #2
    Pedant and proud of it
    Join Date
    Dec 2004
    Location
    Christchurch
    Posts
    6,047

    Default Re: Looping in Python

    I'm not much of a fan for snakes, or any other interpreters, but a for loop is generally quite simple to use.

    you might have a flexible for like
    Code:
    for j = 1,4 ,6, 82, 87, 90
       blah = j;
       blah;
    next j;
    which is an easy way to pass in one variable.

    if you only have the conventional
    Code:
    for i=1 to 13
       blah
       blah=variables[i] + othervariables[i]
    next i
    is one way to pass in variables. Just fill as many arrays as you need, and use the for loop control variable as the index.

  3. #3
    Mike Mike's Avatar
    Join Date
    Dec 2004
    Location
    Rosarito, Mexico
    Posts
    1,382

    Default Re: Looping in Python

    Let me see if I understand you

    If my variables are Bob, Fred, Jack, Mary and I want the script to run based on each of those, I go something like:

    Code:
    for group = Bob, Fred, Jack, Mary
    people = group
     
    run.script(people)
     
    next people
    Would that be right?

    And can I run a loop within a loop? Or is that getting a bit carried away?

    Cheers,
    Mike.
    what are you doing looking in here?

  4. #4
    ~ >*()))><( andrew93's Avatar
    Join Date
    Dec 2004
    Posts
    1,556

    Default Re: Looping in Python

    The for loop in Python doesn't follow the traditional for .... next syntax, it is more like this :

    Code:
    MyArray = ['bird', 'cat', 'dog']
    for MyVariable in MyArray:
         print MyVariable
    There is some good online help available for Python here :

    http://www.faqs.org/docs/diveintopyt...einfo_for.html

    HTH, Andrew

    P.S. Yes you can nest loops inside loops.
    Last edited by andrew93; 24-06-2006 at 12:04 AM.

  5. #5
    Mike Mike's Avatar
    Join Date
    Dec 2004
    Location
    Rosarito, Mexico
    Posts
    1,382

    Default Re: Looping in Python

    Quote Originally Posted by andrew93
    The for loop in Python doesn't follow the traditional for .... next syntax, it is more like this :

    Code:
    MyArray = ['bird', 'cat', 'dog']
    for MyVariable in MyArray:
    print MyVariable
    There is some good online help available for Python here :

    http://www.faqs.org/docs/diveintopyt...einfo_for.html

    HTH, Andrew

    P.S. Yes you can nest loops inside loops.
    Fantastic, thanks Andrew. I'll see if I can get it to work when I'm at work on Monday (unless I log in from home before then ) and hopefully it'll all work out... it seems straightforward to me, just wasn't too sure on the syntax.

    Cheers,
    Mike.
    what are you doing looking in here?

  6. #6
    Pedant and proud of it
    Join Date
    Dec 2004
    Location
    Christchurch
    Posts
    6,047

    Default Re: Looping in Python

    I'd imagine that you can put a for loop inside the script if you want to.

    It's the syntax which will always bite you. That's why I gave one for loop with ";" termninators and one without.

    It seems that Python uses a ":" to open the body. Isn't a terminator required for the loop body? Or does it allow only one statement in the body?

    I'm doing one project in C at the moment. Since I use Pascal mostly, I'm not happy with it. It requires terminators in places which look wrong , and (unlike Pascal) gets upset if you put them where they aren't needed.

  7. #7
    ~ >*()))><( andrew93's Avatar
    Join Date
    Dec 2004
    Posts
    1,556

    Default Re: Looping in Python

    Graham
    Python doesn't use loop terminators, it uses indentation and the full colon to work out if something is inside or outside the loop.
    Andrew
    Last edited by andrew93; 24-06-2006 at 03:17 PM.

  8. #8
    Pedant and proud of it
    Join Date
    Dec 2004
    Location
    Christchurch
    Posts
    6,047

    Default Re: Looping in Python

    WOOHOO. Lots of scope for typographical errors in that. I like begin ... end myself.

  9. #9
    Mike Mike's Avatar
    Join Date
    Dec 2004
    Location
    Rosarito, Mexico
    Posts
    1,382

    Default Re: Looping in Python

    I like the way Python uses indentation as you can just glance at a script and see which things go together.

    Two weeks ago I knew nothing about Python - after a couple of weeks of teaching myself (mostly by initially disecting other scripts and then using the python.org documentation once I figured out mostly how things worked), I think I understand it fairly well, but I've still got a lot to learn.

    Using 'None' instead of 'Null' threw me though - I think it'll take me a while to get used to that one

    Mike.
    what are you doing looking in here?

  10. #10
    ~ >*()))><( andrew93's Avatar
    Join Date
    Dec 2004
    Posts
    1,556

    Default Re: Looping in Python

    Hey Mike

    Python is ok. However, I have used quite a few languages (including Pascall Graham but that was eons ago!) and my preference is visual basic. Well it is at the moment. There is so much more online support / documentation for it given there a millions of users worldwide. Plus with the right version you can create stand-alone executable applications. In my opinion the Python GUI could hardly be described as feature rich and some of the syntax, as GL indicated, is counter-intuitive.

    As you have probably found out support for Python is very limited. If you have Excel on your PC then you can dabble with the VBA version behind Excel (when in Excel press the Alt and F11 keys at the same time to open the vb editor). I used Python for some massive computational processes and it did the job ok. The claim to fame with Python is speed, but I can't envisage situations (for myself, other than that one project) where nanoseconds make a perceptible difference to the user. Have you considered trying visual basic Mike?

    And a question for Graham : given you understand the importance of syntax, why oh why are you providing syntax examples (albeit incorrect) on a language that I don't think you have used???????

    A

Similar Threads

  1. Python Script
    By Faded_Mantis in forum PressF1
    Replies: 1
    Last Post: 18-02-2008, 08:14 AM
  2. xp install looping back to cd instead of SATA
    By mcjelly in forum PressF1
    Replies: 4
    Last Post: 30-08-2005, 11:24 PM
  3. Replies: 1
    Last Post: 01-07-2004, 03:30 PM
  4. Replies: 11
    Last Post: 09-06-2004, 12:44 PM
  5. Python
    By jonzsie in forum PressF1
    Replies: 4
    Last Post: 21-02-2003, 11:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •