if statement error

This is the place for queries that don't fit in any of the other categories.

if statement error

Postby demirayar » Sat Sep 07, 2013 8:42 pm

Hi everyone

I am new in the forum, also python programming and trying to learn it. I confused the the part code given below. It is simple but when I want to use "compare_2_data" or "compare_3_data" out of "if statement", the compiler says "compare_1_data" is not defined. But I can see the value of this variable when I put "print (compare_1_data)" inside if statement.

Code: Select all
        if startReading_1 !=-1:
                    data_text_3 = data_text_1[text_1_counter].split(' ')
                     compare_1_data = data_text_3 [4]
                     compare_3_data = data_text_3 [6]
                     print (compare_1_data)


Can anyone help me to solve this problem?
Last edited by demirayar on Sat Sep 07, 2013 8:50 pm, edited 1 time in total.
demirayar
 
Posts: 5
Joined: Sat Sep 07, 2013 8:31 pm

Re: if statement error

Postby metulburr » Sat Sep 07, 2013 8:47 pm

the compiler says "compare_2_data" is not defined

from the code shown, the variable compare_2_data is not defined.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1315
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: if statement error

Postby demirayar » Sat Sep 07, 2013 8:49 pm

sorry, it will be "compare_1_data". I write wrong variable
demirayar
 
Posts: 5
Joined: Sat Sep 07, 2013 8:31 pm

Re: if statement error

Postby metulburr » Sat Sep 07, 2013 8:57 pm

compare_1_data is really just an index of a list, the only thing i can see is if the inital string has 2 or more spaces in a row, it is going to split those into the list as empty strings. If you were to print the var data_text_3, it would show whether there is an empty string in there or not.

Without giving us the actual string you are splitting, that is all i could guess.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1315
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: if statement error

Postby stranac » Sat Sep 07, 2013 9:11 pm

My guess would be that the if block is not executed, so the variable is not defined.
But it's hard to say without seeing more of the code(you could post all of it, if it's not too large).
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: if statement error

Postby demirayar » Sat Sep 07, 2013 9:24 pm

Code: Select all
filename_1  = open("text_1.txt","r")
read_text_1 = filename_1.read()
data_text_1 = read_text_1.split("\n")

filename_2  = open("text_2.txt","r")
read_text_2 = filename_2.read()
data_text_2 = read_text_2.split("\n")

text_1_counter = 0
text_2_counter = 0

data_1_counter = 0
data_2_counter = 0


length_text_1 = len(data_text_1)
length_text_2 = len(data_text_2)

while True:

    if text_1_counter < length_text_1:
        startReading_1 = data_text_1[text_1_counter].find("route vrf")
   
        if startReading_1 !=-1:
            data_text_3 = data_text_1[text_1_counter].split(' ')
            compare_1_data = data_text_3 [4]
            compare_3_data = data_text_3 [6]
   
    if text_2_counter < length_text_2:
        startReading_2 = data_text_2[text_2_counter].find("route vrf")

        if startReading_2 !=-1:
            data_text_4 = data_text_2[text_2_counter].split(' ')
            print (data_text_4)
            compare_2_data = data_text_4 [0]
            compare_4_data = data_text_4 [0]
           
 
    text_1_counter = text_1_counter + 1
    text_2_counter = text_2_counter + 1
   



First i read the rows include "route vrf" in text 1 and text 2. These rows includes numbers. For example: route vrf 10.123.32.64 255.255.255.248 10.123.134.68

Then i compare these number (10.123.32.64 etc.) in text 1 and text 2. I split the numbers but can't compare, because the compiler say it is not defined.
demirayar
 
Posts: 5
Joined: Sat Sep 07, 2013 8:31 pm

Re: if statement error

Postby metulburr » Sat Sep 07, 2013 10:14 pm

Im not sure exactly what it is you doing:
your code looks convoluted for a python program. It more looks like you converted c/c++ to python than actual started from scratch of python code.

If i am correct, this little code does essentially what your entire code is doing (for the execption of reading in 2 files)
Code: Select all
#filename = open('text_1.txt')
#data = filename.readlines()

data = [
    "route VRF 10.123.32.64 255.255.255.248 10.123.134.68",
    "route vrf 10.123.32.64 255.255.255.248 20.123.134.68",
    "route vrf 10.123.32.64 255.255.255.248 30.123.134.68"
    ]

for line in data:
    if line.startswith('route vrf'):
        splitline = line.split()
        print(splitline[4])

where data is hardcoded as a list, that the commented code will do. IT is there just to give some data. file.readlines() will read the file into a list split by newline
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1315
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: if statement error

Postby demirayar » Sun Sep 08, 2013 11:28 am

the problem is if I want to print splitline outside of if statement (given below), the compiler doesnt recognize the splitline variable.

Code: Select all
#filename = open('text_1.txt')
#data = filename.readlines()

data = [
    "route VRF 10.123.32.64 255.255.255.248 10.123.134.68",
    "route vrf 10.123.32.64 255.255.255.248 20.123.134.68",
    "route vrf 10.123.32.64 255.255.255.248 30.123.134.68"
    ]

for line in data:
    if line.startswith('route vrf'):
        splitline = line.split()
    print(splitline[4])   # print is written outside of if statement
demirayar
 
Posts: 5
Joined: Sat Sep 07, 2013 8:31 pm

Re: if statement error

Postby hansn » Sun Sep 08, 2013 12:30 pm

demirayar wrote:the problem is if I want to print splitline outside of if statement (given below), the compiler doesnt recognize the splitline variable.

Code: Select all
#filename = open('text_1.txt')
#data = filename.readlines()

data = [
    "route VRF 10.123.32.64 255.255.255.248 10.123.134.68",
    "route vrf 10.123.32.64 255.255.255.248 20.123.134.68",
    "route vrf 10.123.32.64 255.255.255.248 30.123.134.68"
    ]

for line in data:
    if line.startswith('route vrf'):
        splitline = line.split()
    print(splitline[4])   # print is written outside of if statement

Like mentioned earlier, splitline is probably not defined outside of the if statement, as it's only defined if line.startswith('route vrf') == True.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: if statement error

Postby demirayar » Sun Sep 08, 2013 1:26 pm

how i can defined this variable, can you give me an example?
demirayar
 
Posts: 5
Joined: Sat Sep 07, 2013 8:31 pm

Re: if statement error

Postby metulburr » Sun Sep 08, 2013 1:55 pm

the problem is if I want to print splitline outside of if statement (given below), the compiler doesnt recognize the splitline variable.

learn classes

Code: Select all
class Control:
    def __init__(self):
        #filename = open('text_1.txt')
        #data = filename.readlines()
        data = [
            "route VRF 10.123.32.64 255.255.255.248 10.123.134.68",
            "route vrf 10.123.32.64 255.255.255.248 20.123.134.68",
            "route vrf 10.123.32.64 255.255.255.248 30.123.134.68"
            ]
           
        self.lists = []

        for line in data:
            if line.startswith('route vrf'):
                self.splitline = line.split()
                self.lists.append(self.splitline)

app = Control()
for l in app.lists:
    print(l[4])
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1315
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests