| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 70139 | 2006-06-23 05:52:00 | Looping in Python | Mike (15) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 465438 | 2006-06-23 05:52:00 | 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 :D Cheers, Mike. |
Mike (15) | ||
| 465439 | 2006-06-23 06:03:00 | 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 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 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. |
Graham L (2) | ||
| 465440 | 2006-06-23 06:34:00 | 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: 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? :D Or is that getting a bit carried away? Cheers, Mike. |
Mike (15) | ||
| 465441 | 2006-06-23 11:58:00 | The for loop in Python doesn't follow the traditional for .... next syntax, it is more like this : MyArray = ['bird', 'cat', 'dog'] for MyVariable in MyArray: print MyVariable There is some good online help available for Python here : www.faqs.org HTH, Andrew P.S. Yes you can nest loops inside loops. |
andrew93 (249) | ||
| 465442 | 2006-06-23 20:52:00 | The for loop in Python doesn't follow the traditional for .... next syntax, it is more like this : MyArray = ['bird', 'cat', 'dog'] for MyVariable in MyArray: print MyVariable There is some good online help available for Python here : www.faqs.org 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. |
Mike (15) | ||
| 465443 | 2006-06-24 03:09:00 | 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. |
Graham L (2) | ||
| 465444 | 2006-06-24 03:15:00 | 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 |
andrew93 (249) | ||
| 465445 | 2006-06-24 03:29:00 | WOOHOO. ;) Lots of scope for typographical errors in that. I like begin ... end myself. | Graham L (2) | ||
| 465446 | 2006-06-24 04:54:00 | 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. |
Mike (15) | ||
| 465447 | 2006-06-24 06:38:00 | 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 |
andrew93 (249) | ||
| 1 2 | |||||