473,597 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested For and While Statements

I start my code with some constants then a while statement. But I
have some For statements towards the end within the While statement
where I start getting some errors. I'm hoping I won't have to post my
code, but it looks something like this:

Import os, string

while this:

All Code indented like it should
Last line in While.

For i in range(1)
class = []
class2 = []
For i in range(2)
Do this
And this
And that.

Next Line hits snag here? Where should this line
be?

Sep 24 '07 #1
16 2229
ko****@hotmail. com wrote:
I start my code with some constants then a while statement. But I
have some For statements towards the end within the While statement
where I start getting some errors. I'm hoping I won't have to post my
code, but it looks something like this:

Import os, string

while this:

All Code indented like it should
Last line in While.

For i in range(1)
class = []
class2 = []
For i in range(2)
Do this
And this
And that.

Next Line hits snag here? Where should this line
be?
Your for loops both use the same counting index.
Sep 24 '07 #2
On Sep 24, 2:28 pm, kou...@hotmail. com wrote:
I start my code with some constants then a while statement. But I
have some For statements towards the end within the While statement
where I start getting some errors. I'm hoping I won't have to post my
code, but it looks something like this:

Import os, string

while this:

All Code indented like it should

Last line in While.

For i in range(1)
class = []
class2 = []
For i in range(2)
Do this
And this
And that.

Next Line hits snag here? Where should this line
be?
Why are the for loops further indented than the body of the while? If
they are part of the while, "For i in range(1)" should line up with
"Last line in While." If they are not part of the while, but come
after, then "For i in range(1)" should line up with "while this:".

Also, as James Stroud said, your for loop indexes are the same
variable.

-- Paul

Sep 24 '07 #3
Your for loops both use the same counting index.

Since those variables are local to the for loop, theoretically it
should work with both loops using the same variable. Although bad
practice, I tested it on my machine and the following code does indeed
work as expected, so it appears that the problem is indenting the for
loops properly.
for j in range( 10 ):
print j, "first loop"
for j in range( 5 ):
print " ", j, "2nd loop"

Sep 24 '07 #4
In addition, "for" is lower case, i.e not "For". If this doesn't
solve the problem then please post the actual error message. "Next
Line hits snag here? Where should this line be?" is not specific
enough.

Sep 24 '07 #5
On Sep 24, 3:01 pm, Paul McGuire <pt...@austin.r r.comwrote:
On Sep 24, 2:28 pm, kou...@hotmail. com wrote:


I start my code with some constants then a while statement. But I
have some For statements towards the end within the While statement
where I start getting some errors. I'm hoping I won't have to post my
code, but it looks something like this:
Import os, string
while this:
All Code indented like it should
Last line in While.
For i in range(1)
class = []
class2 = []
For i in range(2)
Do this
And this
And that.
Next Line hits snag here? Where should this line
be?

Why are the for loops further indented than the body of the while? If
they are part of the while, "For i in range(1)" should line up with
"Last line in While." If they are not part of the while, but come
after, then "For i in range(1)" should line up with "while this:".

Also, as James Stroud said, your for loop indexes are the same
variable.

-- Paul- Hide quoted text -

- Show quoted text -
My actual code has different variables, but thanks for pointing that
out. I'm so used to coding in VB, where indenting isn't a problem, so
all this indenting is new to me how Python wants it dictated. I made
the changes and still get an error at the same point after the second
For statement? So:

While :
Code under while
More under while.

For (x)
Part of x
Last of x

For(y)
Part of y
Still part of y
last part of y.

New line part of x #where does this belong?
Last line under while not in For #Should go under While, right?
If I want all my code to run under the while, then the While statement
should be the only line of code not indented once I state it then,
correct?

Sep 24 '07 #6
On Sep 24, 3:28 pm, kou...@hotmail. com wrote:
[...] where I start getting some errors.
I'm hoping I won't have to post my code
"Doctor, I'm feeling bad. I hope I won't have to tell you my
symptoms. What do I have?"

Please provide the actual errors and the actual code. It is easier,
less error prone and more useful to copy and paste them instead of
writing some pseudocode. Maybe the repeated index i is a typo you
made when writing this post and the original code is correct, but we
don't have any way to know that.

--
Roberto Bonvallet

Sep 24 '07 #7
On Sep 24, 3:44 pm, Roberto Bonvallet <rbonv...@gmail .comwrote:
On Sep 24, 3:28 pm, kou...@hotmail. com wrote:
[...] where I start getting some errors.
I'm hoping I won't have to post my code

"Doctor, I'm feeling bad. I hope I won't have to tell you my
symptoms. What do I have?"

Please provide the actual errors and the actual code. It is easier,
less error prone and more useful to copy and paste them instead of
writing some pseudocode. Maybe the repeated index i is a typo you
made when writing this post and the original code is correct, but we
don't have any way to know that.

--
Roberto Bonvallet
Here is my code, in the hopes that there is a reason why it isn't
running. Thanks guys.

Option Explicit - 'Sorry so use to writing this!

import os, string

# Get filename to work with
workspace = "c:\\temp"
filename = "\AVC1030708.14 "
doc1 = workspace + filename

onedoc = open(doc1, "r")
rdoc1 = onedoc.read()
doclist1 = string.split(rd oc1, "\n")
for i in range(8):
doclist1.pop(0)

length = int(len(newlist 1))-2
Nscheme = int(filename[12:14])
numlanes = length/Nscheme

site = filename[4:7]
year = filename[7:9]
month = filename[9:11]
Sscheme = str(Nscheme)

# Get Master AVC Table for Lanes & Direction
fileavc = "\AVCMaster "
doc2 = workspace + fileavc

twodoc = open(doc2, "r")
rdoc2 = twodoc.read()
doclist2 = string.split(rd oc2, "\n")
doclist2.pop(0)

# Dictionary of Zeros to add to Class Fields
addzeros = {1:"0", 2:"00", 3:"000", 4:"0000"}

while doclist1:

list1 = doclist1[0]
doclist1.pop(0)
newlist1 = list1.split("," )
date1 = newlist1[0]

if len(newlist1[0])== 8:
day = date1[2:3]
else:
day = date1[2:4]

if len(day)>1:
day = "0" +1

hour = newlist1[1]

lanex = []
# Find number of Lanes & Fields
if numlanes == 2:
lane1 = newlist1[2:2+Nscheme]
lanex.append(la ne1)
lane2 = newlist1[2+Nscheme:2+Nsc heme*2]
lanex.append(la ne2)
else:
lane1 = newlist1[2:2+Nscheme]
lanex.append(la ne1)
lane2 = newlist1[2+Nscheme:2+Nsc heme*2]
lanex.append(la ne2)
lane3 = newlist1[2+Nscheme*2:2+N scheme*3]
lanex.append(la ne3)
lane4 = newlist1[2+Nscheme*3:2+N scheme*4]
lanex.append(la ne4)

doctemp = doclist2
listtemp = doctemp[0]
avclist = listtemp.split( ",")

while site <avclist[0]:
doctemp.pop(0)
listtemp = doctemp[0]
avclist = listtemp.split( ",")

dirx = []
fhlanex = []

dirx.append(avc list[2])
fhlanex.append( avclist[3])
dirx.append(avc list[5])
fhlanex.append( avclist[6])

if avclist[8] <"0":
dirx.append(avc list[8])
fhlanex.append( avclist[9])
dirx.append(avc list [11])
fhlanex.append( avclist[12])
for x in range(1, numlanes+1):
avcclass = []
Nlanex = []
for i in range(1,Nscheme +1):
numzeros = 5 - int(len(lanex[i-1]))
avcclass.append (addzeros[numzeros] + lanex[i-1])
Nlanex.append(i nt(lanex[i-1])

lanex.pop(0)
totalclass = sum(Nlanex)
Stotalclass = str(totalclass)
#Create 90 Character File for SAS Job
classsum = avcclass[0] + avcclass[1] + avcclass[2] +
avcclass[3] + avcclass[4] + avcclass[5] \
+ avcclass[6] + avcclass[7] + avcclass[8] + avcclass[9] +
avcclass[10] \
+ avcclass[11] + avcclass[12] + avcclass[13]
classmn = "C27" + site + dirx[x] + lanex[x] + year + month +
day + hour + Stotalclass + classsum
break

avcsasdoc = workspace + "\avcsas.tx t"
fdoc = open(avcsasdoc, "w")
fdoc.write(clas smn)

Sep 24 '07 #8
ko****@hotmail. com a écrit :
(snip)
My actual code has different variables, but thanks for pointing that
out. I'm so used to coding in VB, where indenting isn't a problem, so
all this indenting is new to me how Python wants it dictated.
statement:
block line 1
block line 2
# end statement block
I made
the changes and still get an error at the same point after the second
For statement? So:

While :
Code under while
More under while.

For (x)
Part of x
Last of x

For(y)
Part of y
Still part of y
last part of y.
# end for(y)
New line part of x #where does this belong?
# end for(x)
Last line under while not in For #Should go under While, right?
# end while
Sep 24 '07 #9
On Sep 24, 4:52 pm, kou...@hotmail. com wrote:
Here is my code, in the hopes that there is a reason why it isn't
running. Thanks guys.
[code snipped]

You're missing the closing parenthesis on the last line of the inner
for loop.

Richard

Sep 24 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6440
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
2
4298
by: Jim Irvine | last post by:
Does anybody know the limit of nested iif statements you can use in Access?
10
3205
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
5
1323
by: Atley | last post by:
I am trying to set up a nested collection so I can run statements like this: me.lblquestions.text = mysteps.item(1).questions(1).Asked Any ideas on how to do this? I have created a Class(Steps) with a nested Class(Questions) inside of it and a Collection of that Class(Steps) and it works fine, but the Nested Collection(Questions) is escaping me. Help!
1
2445
by: redpayne | last post by:
Ok-I am doing homework out of a book and the instructions are to display an interface with 5 option buttons in a frame. When clicked, each button changes the background color of the frame. It proceeds to tell me to construct CheckboxGroup, use FlowLayaout and add the Checkboxes to the frame along with ItemListener. It says addWindowListener()method, write code for itemStateChanged() which uses the getState() method and nested if statements....
1
3207
by: sql_er | last post by:
Hi all, I am trying to convert an SQL statement into an XPath (or a sequence of XPath) statements. More specifically, I have the following: SELECT a FROM b WHERE c IN (SELECT d FROM e) I recently learned about XPath and know how to perform simple filtering
2
7223
by: pradeep.thekkottil | last post by:
I'm setting up an auction website using PHP and MySQL. There in the section where logged in members can put up new auction in a form, I want to run a form validation where I used if else statements to check the fileds filled. In the form page there are two radio buttons - fixed and auction - (only one can be chosen) and depend upon which one is chosen some text should be inserted in the text fields. For that I'm using a validation where...
1
1383
by: RAJ | last post by:
hi there, can anybody suggest me best sites for learning php with nested sql statements. i need sql query for searching from database with lots of options like using AND, OR etc even using multiple tables thanks, raj
0
1281
by: Neil Cerutti | last post by:
The docs say: A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn't be clear to which if clause a following else clause would belong: if test1: if test2: print x
4
2659
by: Patrick A | last post by:
All, I rely on nested IF statements with multiple conditions heavily, and someone suggested recently writing the statements (and especially reading them months later) would be much easier if I used Case statements instead. I've read several different examples of Case statements, but can't quite figure out the syntax, or if they can be used when you need to test for a pair of conditions, as I am doing below.
0
7959
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8263
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5842
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3876
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1492
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.