2012-11-30 40 views
0

我想創建一個Python程序,執行以下操作:需要幫助完成一個Python程序分級

  1. 詢問用戶的測試,作業,測驗和實驗室在他們 課程的數量。
  2. 詢問用戶是否有與上述測試分開的重量的最後一個,例如,一個 當然有2個測試,每個測試重量12.5%,1個最終重量15%。
  3. 對於每個類別的編號> 0 a。提示用戶的加權百分比,100%,這應該總共 所有類別的100%! b。獲取該分類的分數。 c。如果該類別是實驗室,則將所有分數相加。 d。否則,平均得分。 e。計算該類別的加權平均值。
  4. 使用每個類別的加權平均值計算課程中的等級。
  5. 詢問用戶他/她是否想爲另一個班級計算成績。
  6. 如果用戶迴應是,則返回步驟1.
  7. 否則,結束程序。

我有什麼到目前爲止輸入部分:

tests = raw_input("Enter the number of tests in course: ") 
tests = int 
assignments = raw_input("Enter the number of assignments in course: ") 
quizzes = raw_input("Enter the number of quizzes in course: ") 
labs = raw_input("Enter the number of labs in course: ") 
sepweightfinal = raw_input("Is there a final with a separate weight? ") 

當我嘗試輸入後做什麼我不能得到它的工作。

比如如果測試> 0%的輸入=(「什麼是測試的加權百分比?:」)

我的程序總是說> 0是無效的,是有辦法做到這一點?

在此先感謝,基本上我瞭解邏輯和我想完成的任務,代碼只是不點擊我的腦海。

+0

你能後的*所有*您的密碼? – Blender

+3

這是班級作業的作業嗎? –

+0

這很不幸,我只有這些。我是python的新手。輸入部分後,我試圖添加「如果測試> 0 raw_input(」輸入測試的加權百分比:「)我會怎麼去做每個部分? –

回答

1

你需要使用intraw_input()返回的字符串轉換爲整數:

tests_string = raw_input("Enter the number of tests in course: ") 
tests = int(tests_string) 

或者更簡潔:

tests = int(raw_input("Enter the number of tests in course: ")) 
0

使用另一個變量的加權百分比如

if test>0: 
    test_weight=float(raw_input("Enter the weight of the tests:") 
else: 
    test_weight=0 
if assignments>0: 
    assign_weight=float(raw_input("Enter the weight of the assignments:") 
else: 
    assign_weight=0 

然後chk如果所有權重的總和是等於100

if (test_weight+assign_weight+lab_weight+final_weight)!=100: 
    print "The weights are not accurate" 
    break 

使用浮點數來獲得每一個的得分和for循環的測試次數。

for i in range(0,test): 
    test_score.append(float(raw_input("Enter the score for the test:"))) 

哪裏test_score是,你必須列出了分數的不同組件的所有分數,你可以計算總和,平均等與他們的檔次和最終使用if語句計算list.Now。

if weightedavg>=60: 
    grade='A' 
elif weightedavg>=40: 
    grade='B' 
else: 
    grade='C' 

放在一個while循環

while(1): 

整個事情並打破循環,如果用戶reponds沒有爲6點

if user_resp=='N': 
    print "Goodbye" 
    break 

爲了更好的可讀性,而不是有許多變量i會建議你把所有的數據放入一個帶有鍵的字典中作爲課程的不同組成部分。處理有組織的東西會容易得多。

+0

有了這個,你必須能夠編寫你自己的代碼,因爲我已經給出了代碼的每一部分的例子。而且你不要求在這裏完整的代碼。搜索互聯網,如果你仍然有疑問,或者如果代碼給出錯誤,你可以問它。這不是「我無法寫出它,有人可以爲我做,請」的地方。 – Tyranicangel

+0

謝謝你的幫助,你的解釋會對我有很大的幫助。我很抱歉,如果它看起來像我想要一個完整的程序的所有代碼,我只是編程的新手,我不知道如何做到這一點。任何谷歌搜索基本上是一般的概念,對這個程序很少應用。學習過程時解釋是非常有用的=) –

0

這應該做的伎倆:

tests = int(raw_input(" Enter the number of tests in course: "))