multiple raw_inputs to a for-loop

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

multiple raw_inputs to a for-loop

Postby fishon91 » Thu Mar 28, 2013 2:15 am

Hi all, first post here! I just completed the course at codeacademy.com. neat little course.
Anyway, I'm onto my first project, making a GPA calculator. first issue ive run into is that I have my variable:

numbers = int(raw_input("How many classes have you taken"?))

then I need to add things to another list, in the form of: classes = (class name1, credit hours1, grade1, class name2, credit hours2, grade2, etc...)
to do this I set up a range loop:

for i in range("numbers"):
classes = raw_input("For one class enter the class name, credit hours and GPA \n Each separated by a comma \n")

The issue it's giving me, is that if I print classes to check my code, classes only holds the information for the last class entered. So if there is more than one class, it will not work. Ive tried multiple other things... .append, str(), int()...

any help completing the track im on, or giving me the creativity to try a different direction would help. thank you
fishon91
 
Posts: 4
Joined: Thu Mar 28, 2013 1:47 am

Re: multiple raw_inputs to a for-loop

Postby micseydel » Thu Mar 28, 2013 6:19 am

Please post the exact code (in code tags!) to reproduce the problem, as well as the input and output, plus the desired output.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1368
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: multiple raw_inputs to a for-loop

Postby fishon91 » Thu Mar 28, 2013 7:39 pm

code:
Code: Select all
#clear screen
print "\n" * 20

#ask user how many classes they've taken
number = int(raw_input("How many classes have you taken. Include passed and withdrawed courses."))

#make a loop to add  class name, credit hours and grade to list
for i in range(number):
    number = raw_input("input class info in form: \nclass name, credit hours, grade\n")
   
print number


First input: 2
Second input: Eng 101, 3, C
Third input: Engr 109, 2, B

Result: Engr 109, 2, B
desired result: Eng 101, 3, C, Engr 109, 2, B
fishon91
 
Posts: 4
Joined: Thu Mar 28, 2013 1:47 am

Re: multiple raw_inputs to a for-loop

Postby stranac » Thu Mar 28, 2013 9:31 pm

Use a list instead of overwriting number every time.
Code: Select all
classes = []
number_of_classes = int(raw_input("Classes taken: "))

for i in xrange(number_of_classes):
    classes.append(raw_input('Enter class info: '))

for c in classes:
    print c

For your example inputs, the output is:
Code: Select all
Eng 101, 3, C
Engr 109, 2, B
Friendship is magic!

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

Re: multiple raw_inputs to a for-loop

Postby fishon91 » Thu Mar 28, 2013 9:59 pm

Aaaahhh! I see! thank you ver much. While Ive got you here, let me try to figure this out a little more. Why is it printing out each class info on separate lines on your code?
IOW, why are you getting this result:
Code: Select all
eng 101, 2, C
engr 102, 3, d


I would have expected each string between commas would have printed on its own line.
fishon91
 
Posts: 4
Joined: Thu Mar 28, 2013 1:47 am

Re: multiple raw_inputs to a for-loop

Postby stranac » Thu Mar 28, 2013 10:09 pm

'eng 101, 2, C' is a single input, so it's stored in a single string.
If you want to get multiple values from one raw_input(), you'll have to split the returned string(e.g. using str.split())
Friendship is magic!

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

Re: multiple raw_inputs to a for-loop

Postby fishon91 » Sun Mar 31, 2013 3:16 pm

Got it. I think I will have to do that. thank you all.
fishon91
 
Posts: 4
Joined: Thu Mar 28, 2013 1:47 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests