Variable Assignment in a loop as well creating a list..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlandry287
    New Member
    • Apr 2013
    • 3

    #1

    Variable Assignment in a loop as well creating a list..

    Code:
    a=raw_input('Enter the number of course you are taking: ')
    b=int(a)
    c=range(b)
    if int(a) <= 0:
          print ('Error: You must have at least one class!')
    d=('Enter the course *NAME*, *CREDITS*, and *GRADE*: ')
    #
    for i in range(b):
        locals()["k"+str(i)]=locals()["y"+str(i)]=raw_input(d)
    That is the code I have so far. I am looking for an easier way to do this, as i have to calculate GPA from the user input.

    What i need to do:
    *be able to extract the grade and credits from a list, therefor be able to make ANOTHER loop for the variables
    ex:
    if b==1:
    print y0
    elif b==2:
    print y0
    print y1
    Last edited by Rabbit; Apr 8 '13, 04:33 PM. Reason: Please use code tags when posting code.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I would compile a list of user input and iterate over the list to calculate the GPA. The following uses a list comprehension (untested):
    Code:
    data = ()
    for i in range(b):
        data.append([raw_input("Enter name, credits and grade separated by commas").split(",")])
    
    GPA = sum([int(item[2]) for item in data])/float(b)

    Comment

    Working...