•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 402,750 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,436 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 3845 | Replies: 14
![]() |
•
•
Join Date: Apr 2005
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
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!
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation:
Rep Power: 9
Solved Threads: 173
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 ...
[php]# 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
[/php]
[php]# 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
[/php]
May 'the Google' be with you!
•
•
Join Date: Apr 2005
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Originally Posted by vegaseat
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 ...
[php]# 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
[/php]
It is text and numbers. It is a listing of like 10 people, their major, gpa, and student id number.
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation:
Rep Power: 9
Solved Threads: 173
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 ...
[PHP]# 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
[/PHP]
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.
[PHP]# 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
[/PHP]
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.
May 'the Google' be with you!
•
•
Join Date: Apr 2005
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
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 = ['5555', '2323', '1001', '9999', '3428', '5920', '3012', '8954', '7834', '3490']
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.
Idnum = ['5555', '2323', '1001', '9999', '3428', '5920', '3012', '8954', '7834', '3490']
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.
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation:
Rep Power: 9
Solved Threads: 173
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 ...
[PHP]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
[/PHP]
[PHP]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
[/PHP]
May 'the Google' be with you!
•
•
Join Date: Apr 2005
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
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 = ['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']
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!
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']
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!
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation:
Rep Power: 9
Solved Threads: 173
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 ...
[php]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
[/php]
Do all of us a favor and put your code into a code or php field!
See: http://www.daniweb.com/techtalkforum...cement8-3.html
[php]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
[/php]
Do all of us a favor and put your code into a code or php field!
See: http://www.daniweb.com/techtalkforum...cement8-3.html
May 'the Google' be with you!
•
•
Join Date: May 2005
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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.
[PHP]
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
[/PHP]
output:
Nancy Doe
4.0
Eric Adams
3.8
Marilyn Monroe
3.5
Rory Gilmore
4.0
Brad Paisley
3.6
[PHP]
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
[/PHP]
output:
Nancy Doe
4.0
Eric Adams
3.8
Marilyn Monroe
3.5
Rory Gilmore
4.0
Brad Paisley
3.6
Last edited by kyle.tk : May 1st, 2005 at 2:49 pm. Reason: oops he beat me to it
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
api apple asm assembly x86 programming hla demo blogger blogging c c++ ccna cocoa code coding competition compiler compilers computer debugging developer development errors evaluation framework gdata google high-performance innovation java languages linerider mcse microsystems networking news next object online open oriented planning platform practices programming python rss software step steps sun tools tutorials xml
- Need help with C programming assignment (will pay money) (C)
- please i need help in my C++ assignment (C++)
- Virus Programming (Assembly)
- OO assignment help (Java)
- Windows programming task (C)
- i need help getting started with my programming assignment (C++)
Other Threads in the Python Forum
- Previous Thread: What are the .apy files in Python
- Next Thread: convert python to java



Linear Mode