[英]I need help finishing a grade calculator program in python
對於我的計算機科學課程,我必須編寫一個可以計算成績的程序。 這是我的第一門計算機科學課(以前沒有經驗),所以我正在努力學習。
這些是方向:
向用戶詢問其課程中的測試,作業,測驗和實驗室的數量。
詢問用戶是否有與上述測試有不同權重的決賽,例如,一門課程有2項測試,每項稱重為12.5%,有1項最終稱重為15%。
對於數字大於0的每個類別
一種。 提示用戶輸入100%中的加權百分比,所有類別的總百分比應為100%!!!
b。 獲取該類別的分數。
C。 如果類別是實驗室,則將所有分數相加。
d。 否則,取平均分數。
e。 計算類別的加權平均值。
使用每個類別的加權平均值,計算課程中的成績。
詢問用戶他/她是否要計算另一個班級的成績。
如果用戶回答是,則返回步驟1。
否則,結束程序。
到目前為止,我的代碼:
def main():
lists = get_user_input()
get_scores(lists);
get_weighted_average(lists)
def get_user_input():
# How many?
t = int(input("How many tests?: "))
a = int(input("How many assignments?: "))
q = int(input("How many quizzes?: "))
l = int(input("How many labs?: "))
# How much weight on grade?
tw = float(input("Enter weight of tests: "))
aw = float(input("Enter weight of assignments: "))
qw = float(input("Enter weight of quizzes: "))
lw = float(input("Enter weight of labs: "))
lists = [t, a, q, l, tw, aw, qw, lw]
return lists
def get_scores(lists):
# What are the scores?
scores = [0] * 5
for(x in range(lists[0]):
test_scores = float(input("enter your test scores: "))
scores[x] = test_scores
for(x in range(lists[1]):
test_scores = float(input("enter your assignment scores: "))
scores[x] = assignment_scores
for(x in range(lists[2]):
test_scores = float(input("enter your quiz scores: "))
scores[x] = quiz_scores
for(x in range(lists[3]):
test_scores = float(input("enter your lab scores: "))
scores[x] = lab_scores
sumlabs = 0
for(x in range(lists[3]):
sumlabs = sumlabs + scores[x]
print(sumlabs)
def get_weighted_average(lists):
main()
我不確定該如何進行,因此不勝感激。
平均分數意味着將它們相加並除以它們的數量。 您可以使用列表可以告訴您列表中有多少個元素的事實。 例如,如果x是一個列表,則len(x)是列表中的事物數。 您應該小心不要嘗試計算空列表的分數,因為那意味着要除以零。 在以下函數中,如果列表中有內容,則'if score_list:'將為true。 否則,它返回None(因為未定義平均值)。
def average_score(score_list):
if score_list: # makes sure list is not empty
total = 0 # our total starts off as zero
for score in score_list: # starts a loop over each score
total += score # increases the total by the score
return total / len(score_list) # returns aveage
要根據用戶輸入形成一個分數列表,假設用戶在提示輸入分數時將它們全部放在同一行,則可以從用戶那里得到一個字符串(大概是:“ 13.4 12.9 13.2”,然后使用字符串的split方法)將其轉換為諸如[“ 13.4”,“ 12.9”,“ 13.2”]之類的字符串列表,然后將該字符串列表轉換為浮點數列表。最后,使用上述平均函數,您可以獲得平均值:
test_score_entry = raw_input("Enter your test scores separated by spaces: ")
test_sore_strings = test_score_entry.split()
test_scores = [float(_) for _ in test_score_strings]
average_score = average(test_scores)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.