llovering
Posts: 6
Joined: Sat Jul 14, 2012 11:13 pm

For-Next loop

Fri Jul 18, 2014 9:35 pm

I'm new to Python and have a PiFace attached to my Pi. I want to do these things:

1. Enter a number, either 40, 80 or 140
2. Until X = number
3. Do Pi stuff
4. Next X
5. Goto 1 ; (line 1, enter a number).

I can do the loop but can't figure out how to go back to the number question.
Thank you,
Larry

User avatar
David Melara
Posts: 26
Joined: Sun Jul 13, 2014 8:32 pm
Location: El Salvador
Contact: Website

Re: For-Next loop

Fri Jul 18, 2014 9:54 pm

The goto does not existe anymore, so in the nowdays programming languages, you use loops,
while (1): //means is going to loop forever
Do your code
Son when your code finish, it will go back to the start, otherwise, you can use functions, like
def say_hi:
Print "Hi"

While (1):
Ask a number
If number == 1
say_hi

it will do what the functions is programmed to do, in this case, it will print Hi when you call it.

good luck!
Last edited by David Melara on Fri Jul 18, 2014 11:47 pm, edited 1 time in total.
If you love doing something, be sure that something will be done right

llovering
Posts: 6
Joined: Sat Jul 14, 2012 11:13 pm

Re: For-Next loop

Fri Jul 18, 2014 10:24 pm

So, can I nest a for-next loop into this, like this:

While(1):
Ask for the number
For x=1 to x
Do Pi stuff
Next x

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: For-Next loop

Fri Jul 18, 2014 10:36 pm

llovering wrote:So, can I nest a for-next loop into this, like this:

While(1):
Ask for the number
For x=1 to x
Do Pi stuff
Next x
Yes.

User avatar
PeterO
Posts: 5084
Joined: Sun Jul 22, 2012 4:14 pm

Re: For-Next loop

Sat Jul 19, 2014 6:28 am

If you are talking about python programming, you ought to be taking the effort to atleast lay out your code in a Python style.....
Use the "Code" button above the text entry box to preserve the layout of the text you type.... like this

Code: Select all

I typed this text
   Then highlighted it with the mouse
   Then pressed the "Code" button
       And as you can see the indentation has been preserved.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5084
Joined: Sun Jul 22, 2012 4:14 pm

Re: For-Next loop

Sat Jul 19, 2014 6:43 am

ame wrote:
llovering wrote:So, can I nest a for-next loop into this, like this:

While(1):
Ask for the number
For x=1 to x
Do Pi stuff
Next x
Yes.
No, not like that. Since we are talking about Python, then the examples you give should be as close to valid Python code as possible. And when you use something that isn't Python code it should be
clear that it isn't so that less experienced programmers don't think it is !

What you want is

Code: Select all

while True:
    Some code goes here  to ask for a number and assign it to n
    for x in range(n):
        Some code goes here to do stuff goes 
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

llovering
Posts: 6
Joined: Sat Jul 14, 2012 11:13 pm

Re: For-Next loop

Sat Jul 19, 2014 10:48 am

Ok, here's my code but I haven't found a "sleep" function in Python; is there a substitute?

Code: Select all

Import pifacedigitalio as pfio
Pfio.init()

while True:
   a = int(raw_input("Enter the number of slides [40,80,160: "))
    for x in range(a):
          pfio.digital_write(0,1) # Drop slide
	  pfio.digital_write(0,0)
	  sleep 2 # wait for focus to settle
	  pfio.digital_write(1,1) # Take picture
          pfio.digital_write(1,0)
        sleep 2 # wait for picture to write
    Next x
 

User avatar
joan
Posts: 14376
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: For-Next loop

Sat Jul 19, 2014 10:50 am

Google Python sleep.

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: For-Next loop

Sat Jul 19, 2014 10:55 am

import time

time.sleep(1)
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

llovering
Posts: 6
Joined: Sat Jul 14, 2012 11:13 pm

Re: For-Next loop

Sat Jul 19, 2014 10:58 am

Think I got it...

Code: Select all

Import pifacedigitalio as pfio
import time
Pfio.init()

while True:
   a = int(input("Enter the number of slides [40,80,160: "))
    for x in range(a):
        pfio.digital_write(0,1) # Drop slide
	pfio.digital_write(0,0)
	time.sleep(2) # wait for focus to settle
	pfio.digital_write(1,1) # Take picture
        pfio.digital_write(1,0)
        time.sleep(2) # wait for picture to write
    if x == a 
         break



llovering
Posts: 6
Joined: Sat Jul 14, 2012 11:13 pm

Re: For-Next loop

Sat Aug 02, 2014 4:52 pm

As for the code in the previous example, Python says I have the spacing or indentation wrong. I also had to define true as 1.

User avatar
rpdom
Posts: 15426
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: For-Next loop

Sat Aug 02, 2014 5:09 pm

Well, you have the spacing and indentation wrong then. From the little I know of Python that appears to be the case.

Your lines are wandering in and out for no reason that I can see. Make sure all lines under a test/loop/selector of any sort are indented to the same level.

Code: Select all

while True:
   a = int(input("Enter the number of slides [40,80,160: "))
    for x in range(a):
        pfio.digital_write(0,1) # Drop slide
        pfio.digital_write(0,0)
not

Code: Select all

while True:
   a = int(input("Enter the number of slides [40,80,160: "))
    for x in range(a):
        pfio.digital_write(0,1) # Drop slide
   pfio.digital_write(0,0)
Ending the indent will end the "for" loop.
Then after a few lines you have suddenly put an indent again. Python is wondering why as it can't see any test or loop on the previous line.

Also, on a small bit of test code I ran I didn't have to define "True" as 1, but I would have had to define "true" as 1 (case sensitive).

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: For-Next loop

Sat Aug 02, 2014 5:14 pm

When using Python, you need to get the indentation exactly right, as it uses that to work out where blocks of code (loops, if statements, etc.) stop and start.
I think you want

Code: Select all

import pifacedigitalio as pfio
import time
pfio.init()

while True:
    a = int(input("Enter the number of slides [40,80,160: "))
    for x in range(a):
        pfio.digital_write(0,1) # Drop slide
        pfio.digital_write(0,0)
        time.sleep(2) # wait for focus to settle
        pfio.digital_write(1,1) # Take picture
        pfio.digital_write(1,0)
        time.sleep(2) # wait for picture to write

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: For-Next loop

Sat Aug 02, 2014 5:21 pm

Get rid of the lines...

Code: Select all

if x == a
    break
Not only will it not work without a colon after 'a' but it's completely unnessecary.

Dave.
Apple say... Monkey do !!

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: For-Next loop

Sat Aug 02, 2014 5:33 pm

PeterO wrote:

Code: Select all

Some code goes here to do stuff goes 
Thankyou Master Yoda ;)
Apple say... Monkey do !!

Return to “Python”