By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,609 Members | 1,119 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,609 IT Pros & Developers. It's quick & easy.

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

P: 3
Expand|Select|Wrap|Line Numbers
  1. a=raw_input('Enter the number of course you are taking: ')
  2. b=int(a)
  3. c=range(b)
  4. if int(a) <= 0:
  5.       print ('Error: You must have at least one class!')
  6. d=('Enter the course *NAME*, *CREDITS*, and *GRADE*: ')
  7. #
  8. for i in range(b):
  9.     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
Apr 8 '13 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
I would compile a list of user input and iterate over the list to calculate the GPA. The following uses a list comprehension (untested):
Expand|Select|Wrap|Line Numbers
  1. data = ()
  2. for i in range(b):
  3.     data.append([raw_input("Enter name, credits and grade separated by commas").split(",")])
  4.  
  5. GPA = sum([int(item[2]) for item in data])/float(b)
Apr 8 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.