我最近開始學習Python,並且正忙於爲Codecademy教程進行學習。我剛剛完成了本教程,您將在其中創建一個程序來確定字典中標記的平均值。繼承人的當前代碼:在Python中使用用戶輸入來創建字典
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
class_list = [lloyd, alice, tyler]
def average(numbers):
total = sum(numbers)
total = float(total)
total = total/len(numbers)
return total
def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return homework * 0.1 + quizzes * 0.3 + tests * 0.6
def get_class_average(students):
results = []
for student in students:
results.append(get_average(student))
return average(results)
print get_class_average(class_list
但我想做的事作爲一個擴展的是更加方便用戶通過在使程序要求用戶輸入lloyd
在第一線,以及輸入的所有值友好字典。此外,我想讓程序生成一個新字典,每次用戶輸入字典的名稱時,例如第一行的lloyd
。然後用所有詞典填寫class_list
。最後,我希望把它使用戶可以在該行的標記也輸入權重:
return homework * 0.1 + quizzes * 0.3 + tests * 0.6
我無法這樣做,所以任何幫助,將不勝感激。
這並未看起來對於我來說,像你的問題是,「我如何在Python中接受用戶輸入?」 –
[python:getting user input]的可能重複(http:// stackoverflow .com/questions/3345202/python-getting-user-input) –
請參閱:http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html – 2015-11-06 20:45:37