1

I need to create a code where the user can input a certain number of courses, and then it will take the gpa of them, but how can I change the variable name in the loop? I have this so far

number_courses= float(input ("Insert the number of courses here:"))
while number_courses>0:
    mark_1= input("Insert the letter grade for the first course here: ")
    if mark_1=="A+" :
        mark_1=4.0
    number_courses= number_courses-1

If I want to change the variable name of mark_one to something different each time I go through the loop, what is the simplest way I can do this? And also is it possible to change it in my input statement to ask for first, second, third... as I go through the loop? I have tried searching on google, but none of the answers I can understand as their code is far behind my level or they didn't seem to answer what I needed either. Thanks.

tamasgal
  • 24,826
  • 18
  • 96
  • 135

2 Answers2

2

You want to use a list or something similar to gather the input values:

number_courses=input("Insert the number of courses here: ")
marks = []
while number_courses>0:
    mark = input("Insert the letter grade for the first course here: ")
    if mark == "A+":
        mark = 4.0
    marks.append(mark)
    number_courses -= 1

print marks
tamasgal
  • 24,826
  • 18
  • 96
  • 135
0

Use a dictionary:

number_courses= float(input ("Insert the number of courses here:"))
marks = {'A+':4, 'A':3.5, 'B+':3}
total_marks = 0
while number_courses:
    mark_1= input("Insert the letter grade for the first course here: ")
    if mark_1 in marks:
        total_marks += marks[mark_1] #either sum them, or append them to a list
        number_courses -= 1 #decrease this only if `mark_1` was found in `marks` dict
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • I think the `del marks[mark_1]` is a bug. What it they got the same grade in two courses, or all three in three different ones. – martineau Sep 29 '13 at 18:04