I have a program due and I cannot figure it out! I have read about 30 tutorials to no avail! I need to have data read into python from an input file. The data is suppossed to be info from a pretend university that only has 10 students. Once I figure out how to input the data I should be able to do the rest but right now I am stumped!

You need to let us know what kind of a data file this is. If it is just a text file, you can read the whole thing into a string and go from there ...

# read the entire text from a file into a string
fileHandle = open ( 'test.txt', 'r' )
str1 = fileHandle.read()
fileHandle.close()

# now process the string ...
print str1

You need to let us know what kind of a data file this is. If it is just a text file, you can read the whole thing into a string and go from there ...

# read the entire text from a file into a string
fileHandle = open ( 'test.txt', 'r' )
str1 = fileHandle.read()
fileHandle.close()

# now process the string ...
print str1

It is text and numbers. It is a listing of like 10 people, their major, gpa, and student id number.

I assume from your limited info that each student's data is on a separate line. In this case you can read the file into a list of data lines ...

# read the data from a file into a list of data lines 
fileHandle = open ( 'test.txt', 'r' ) 
dataList = fileHandle.readlines() 
fileHandle.close() 

# now process each data line from the list ... 
print dataList[0]
print dataList[1]
# etc

You can treat each line as a string and separate it with the split() function at the delimiter (space, tab, comma, whatever?) into name, study, gpa and ID.

So I talked with my teacher and he told me I did my last project wrong (the program i asked about previously is a continuation of my last project). I was working on it again tonight following the steps my teacher told me and I cannot figure out why it will not work. Here is the code I was told to use:

Idnum =

task = input("Press 1 to select a student: ")
if task == 1 :
student = input("Enter student id number: ")
count = 0
while (count < len(Idnum)) :
if student == Idnum[count] :
index = count
count = count+1
print Indnum == index


Where is the mistake? When I run it the program does nothing, it doens't close or anything, just sits there.

The numbers in your Idnum list are strings. Input() gives you an integer number. Now you are trying to compare a string with an integer, that will always be false! Use raw_input() to get a string ...

Idnum = ['5555', '2323', '1001', '9999', '3428', '5920', '3012', '8954', '7834', '3490']

task = input("Enter 1 to select a student: ")
if task == 1 : 
    student = raw_input("Enter student id number: ")
    count = 0
    while (count < len(Idnum)) :
        if student == Idnum[count] :
            index = count
            print "Found %s at index = %d" % (student, index)
        
        count = count+1

Thank you so much for your help! It really helped me figure out almost the rest of the program. But I am stuck again. On this part I have an array with GPA's for the students and I need to print a list of students who are on the Dean's List. This is what I thought would do it:

GPA =

Name =

if task == 3 :
count = 0
while (count < len(GPA)) :
index = count
if 3.4 < GPA[index] :
print Name[index]
print GPA[index]
count = count+1

It keeps printing out the entire list though. Thanks for any help you can give!

Again, your GPA list consists of strings and you are comparing this with a floating point number. Convert the string to a float with float() and then compare ...

GPA = ['3.2', '4.0', '3.8', '1.2', '3.5', '4.0', '3.4', '1.4', '3.6', '2.7']

Name = ['John Smith', 'Nancy Doe', 'Eric Adams', 'Homer Simpson', 'Marilyn Monroe', 'Rory Gilmore', 'Gavin Degraw', 'Ashley Simpson', 'Brad Paisley', 'Dr. Phil']

task = 3  # for testing
if task == 3 :
    count = 0
    while (count < len(GPA)) :
        index = count
        # not quite sure if you want to include 3.4
        if  float(GPA[index]) >= 3.4:
            print Name[index]
            print GPA[index]
        
        count = count+1

Do all of us a favor and put your code into a code or php field!
See: http://www.daniweb.com/techtalkforums/announcement8-3.html

your problem again was that you were comparing a string to a float. heres how to convert the string to a float so that you can compare them.

GPA = ['3.2', '4.0', '3.8', '1.2', '3.5', '4.0', '3.4', '1.4', '3.6', '2.7']

Name = ['John Smith', 'Nancy Doe', 'Eric Adams', 'Homer Simpson', 'Marilyn Monroe', 'Rory Gilmore', 'Gavin Degraw', 'Ashley Simpson', 'Brad Paisley', 'Dr. Phil']

count = 0
while (count < len(GPA)):
	index = count
	if float(GPA[index]) > 3.4:
		print Name[index]
		print GPA[index]
	count = count+1

output:
Nancy Doe
4.0
Eric Adams
3.8
Marilyn Monroe
3.5
Rory Gilmore
4.0
Brad Paisley
3.6

Greetings kyle.tk ---

Glad to see you take part!!!!!!!!!!!

Ok so I am about fed up with this program. Everytime I move one step ahead, I get stuck again on something else. Now in the program I am taking the arrays I made and I need to input them into my program. I can input them, but then I can't use them for anything. I tried to figure out the split() thing but I couldn't get it to work. If you know a good website that will explain that to me that would be great. Here is what I have so far:

fileHandle = open ( 'stuff.txt', 'r' )
data= fileHandle.readlines()
Idnum = data[0]
Name = data[1]
Major = data[2]
GPA = data[3]
fileHandle.close()


task = input("Press 1 to select a student.  Press 2 to alter GPA.  Press 3 to print Dean's List.")
if task == 1 :
    student = raw_input("Enter student id number: ")
    count = 0
    while (count < len(Idnum)) :
            if student == Idnum[count] :
                index = count
                print Idnum[index]
                print Name[index]
                print Major[index]
                print GPA[index]
            count = count+1

And the file I am inputting is:
5555 2323 1001 9999 3428 5920 3012 8954 7834 3490
John Smith_Nancy Doe_Eric Adams_Homer Simpson_Marilyn Monroe_Rory Gilmore_Gavin Degraw_Ashley Simpson_Brad Paisley_Dr. Phil
Computer Science_Electrical Engineering_Biology_Physical Education_Communications_Pre-Law_Music_Chorus_History_Psychology
3.2_4.0_3.8_1.2_3.5_4.0_3.4_1.4_3.6_2.7

I promise this is the last question I will have! Thanks for all of your help!

You are making your life harder than it should be. Don't use spaces or underlines as delimiters, but commas. This is far more common and gives you a list of sublists. This way the same index would relate across the sublists.

For instance index 1 would be always the second element in all the sublists, just as you use in your while loop. So 2323 would be Nancy Doe a major in Electrical Engineering with a GPA of 4.0

All you have to work on is assigning one list to another, otherwise your program is ready to go!

Ouch!!!! My booboo, what you are reading in is a list of strings. Got to convert each line to a list. Just a moment, I have to work this out, should be easy!

I am really confused.

Ah, here it is. Minor surgery as they say ...

fileHandle = open ( 'stuff.txt', 'r' )
# read into a list of dataline strings
data = fileHandle.readlines()
fileHandle.close()
# slice off the trailing new line character of each line
data[0] = data[0][:-1]
data[1] = data[1][:-1]
data[2] = data[2][:-1]
data[3] = data[3][:-1]
# convert to sublists, split at the commas
Idnum = data[0].split(",")
Name = data[1].split(",")
Major = data[2].split(",")
GPA = data[3].split(",")
# test it ...
print Idnum
print Name
print Major
print GPA

# this may be a little too much text for the input prompt
#task = input("Press 1 to select a student.  Press 2 to alter GPA.  Press 3 to print Dean's List.")

task = 1  # testing
if task == 1 :
    student = raw_input("Enter student id number: ")
    count = 0
    while (count < len(Idnum)) :
            if student == Idnum[count] :
                index = count
                print Idnum[index]
                print Name[index]
                print Major[index]
                print GPA[index]
            count = count+1

... and here is the modified data file:

5555,2323,1001,9999,3428,5920,3012,8954,7834,3490
John Smith,Nancy Doe,Eric Adams,Homer Simpson,Marilyn Monroe,Rory Gilmore,Gavin Degraw,Ashley Simpson,Brad Paisley,Dr. Phil
Computer Science,Electrical Engineering,Biology,Physical Education,Communications,Pre-Law,Music,Chorus,History,Psychology
3.2,4.0,3.8,1.2,3.5,4.0,3.4,1.4,3.6,2.7

Actually you can use other delimiters, something that doesn't interfere with your data content.

Thank you so so so much for your help! This stuff is actually making sense to me now for the first time in a while! If I ever need more help I know where to come! Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.