User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2005
Posts: 8
Reputation: mneaker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mneaker mneaker is offline Offline
Newbie Poster

Help with programming assignment

  #1  
Apr 26th, 2005
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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: Help with programming assignment

  #2  
Apr 27th, 2005
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]
May 'the Google' be with you!
Reply With Quote  
Join Date: Apr 2005
Posts: 8
Reputation: mneaker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mneaker mneaker is offline Offline
Newbie Poster

Re: Help with programming assignment

  #3  
Apr 27th, 2005
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.
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: Help with programming assignment

  #4  
Apr 28th, 2005
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.
May 'the Google' be with you!
Reply With Quote  
Join Date: Apr 2005
Posts: 8
Reputation: mneaker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mneaker mneaker is offline Offline
Newbie Poster

Re: Help with programming assignment

  #5  
Apr 28th, 2005
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.
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: Help with programming assignment

  #6  
Apr 29th, 2005
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]
May 'the Google' be with you!
Reply With Quote  
Join Date: Apr 2005
Posts: 8
Reputation: mneaker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mneaker mneaker is offline Offline
Newbie Poster

Re: Help with programming assignment

  #7  
Apr 30th, 2005
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!
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: Help with programming assignment

  #8  
May 1st, 2005
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
May 'the Google' be with you!
Reply With Quote  
Join Date: May 2005
Posts: 3
Reputation: kyle.tk is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kyle.tk kyle.tk is offline Offline
Newbie Poster

Re: Help with programming assignment

  #9  
May 1st, 2005
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
Last edited by kyle.tk : May 1st, 2005 at 2:49 pm. Reason: oops he beat me to it
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: Help with programming assignment

  #10  
May 1st, 2005
Greetings kyle.tk ---

Glad to see you take part!!!!!!!!!!!
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 8:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC