tags:

views:

96

answers:

2

Why can't I make multiple assignments under an if statement in python? Is there some syntax I am missing?

I want to do this:

files = ["file1", "file2", "file3"]

print "\nThe following files are available: \n"

i = 0
for file in files:
    i = i + 1
    print i, file

choice = int(raw_input("\Enter a file number: "))

if choice ==1:
    file = np.genfromtxt(files[0], usecols = (1,2,3), dtype = (float), delimiter = '\t')
    time = np.genfromtxt(files[0], usecols = (0), dtype = (str), delimiter = '\t')

print time

Time is defined outside of my if statement, so it doesn't change as choice changes...what the heck?

+1  A: 

Both variables file and time must be defined at an higher block level than your if statement.

Be careful with "time", as it is the name of a python module. You should use a variation of this name (time_ for example).

Guillaume Lebourgeois
A: 

You are not using any other choice except for 1 and it will give error if choice is not 1, you code should be something like this

choice = int(raw_input("\Enter a file number: "))
choice -= 1 # array index is from 0

if choice < 0 or > 2:
    print "Enter correct choice"
    sys.exit()

file = np.genfromtxt(files[choice], usecols = (1,2,3), dtype = (float), delimiter = '\t')
time = np.genfromtxt(files[choice], usecols = (0), dtype = (str), delimiter = '\t')
Anurag Uniyal
ok, that was only one section of the code...I just wanted to know why I cannot assign more than one variable under the if statement...
lollygagger