Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 9 Online
OpenStudy (anonymous):

MOOC Handout 2 sec(2.8) function report_card takes users grades, then program prints out report card w/ GPA. Here is my code so far: def report_card(): classes = int(raw_input("How many classes did you take?")) class_name = raw_input("What was the class name?") grade = raw_input("what was your grade?") data = class_name + " - " + grade my_list = [] my_list.append(data) print my_list report_card() How do I get multiple sets of input? I want the # in "classes" to be the # of times my program asks for raw_input to create a list of grade + class_name

OpenStudy (anonymous):

Well, You would need a loop. Using a loop, a for loop in this case is easy, we can ask a certain amount of time. The one main difference is we are gonna have to move the initialization for your list. def report_card(): my_list = [] classes = int(raw_input("How many classes did you take?")) for i in range(0, classes): class_name = raw_input("What was the class name?") grade = raw_input("what was your grade?") data = class_name + " - " + grade my_list.append(data) print my_list This will run you through the loop the amount of times that is specified. Understand that a for loop has the syntax of for (variable) in (separable data). The variable contains a piece of the data inputted, so if you put a string in there each time you go through it would be a different character, so for instance if you put 'Grandma' in there, the first time through the variable would be 'G', the second time 'r', and so on. With the range function, we can think of it as returning a tuple (0 , ..., classes -1), and the for loop splits each part of the tuple off, so it starts with zero and ends at classes -1. We don't use the variable in this instance, but a for loop is still useful because it only runs a specified amount of times with out having to create a new variable and increment it at the end of the loop. So there's the code and the explanation. Good luck!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!