1

ユーザーが特定の数のコースを入力できるコードを作成する必要があり、それらの gpa が取得されますが、ループ内の変数名を変更するにはどうすればよいですか? 私はこれまでのところこれを持っています

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

ループを通過するたびに mark_one の変数名を別のものに変更したい場合、これを行う最も簡単な方法は何ですか? また、ループを通過するときに、入力ステートメントで最初、2番目、3番目...を要求するように変更することは可能ですか? Googleで検索してみましたが、コードが私のレベルよりはるかに遅れているか、必要なものに答えていないように見えるため、理解できる答えはありません。ありがとう。

4

2 に答える 2

2

リストなどを使用して入力値を収集します。

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
于 2013-09-29T16:57:48.683 に答える
0

辞書を使う:

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
于 2013-09-29T16:58:48.037 に答える