2013-09-29 36 views
1

我需要創建一個代碼,用戶可以在其中輸入一定數量的課程,然後它將佔用它們的gpa,但是如何在循環中更改變量名稱? 我有這個迄今爲止如何在循環中繼續增加可變數字inpython

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的變量名稱更改爲不同的東西,有什麼是我能做到這一點的最簡單方法是什麼?還有可能在我的輸入語句中更改它以詢問第一,第二,第三......當我經歷循環?我試圖在谷歌上搜索,但沒有我能理解的答案,因爲他們的代碼遠遠落後於我的水平,或者他們似乎沒有回答我所需要的答案。謝謝。

+0

這將是簡單了很多供你使用了'名單':這是你用來存儲數據而不是多個變量的東西。 –

+0

您的代碼中沒有任何名爲'mark_one'的變量... ;-) – martineau

回答

2

你想使用類似list或東西來收集輸入值:

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 
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 
+0

我認爲'del marks [mark_1]'是一個錯誤。他們在兩門課程中獲得了相同的成績,或三門不同的三門課程。 – martineau

+0

@martineau好點,修復它。 –