我需要帮助在函数

时间:2017-01-05 14:03:29

标签: python python-2.7 function dictionary

我正在创建一个函数(python 2.7)来创建一个用户输入的字典,其中测试主题为关键,他们的测试分数为值,分数对应的主题是单独提供的列表被称为主题所以我将需要一个for循环循环通过他们轮流询问用户的分数。该功能将返回学生ID和分数。 我认为它是这样的:

subjects = 'english', 'maths', 'science'

dict = {}

     for a in subjects:

         testscore = raw_input("please enter your test score: ")
         dict[a] = testscore

真的很挣扎,任何帮助表示赞赏! 谢谢

1 个答案:

答案 0 :(得分:0)

subjects = ['english', 'maths', 'science']

scores = {}

for a in subjects:
    testscore = raw_input("please enter the score for "+str(a)+": ")
    scores[a] = testscore

subjects必须是一个列表(或任何其他可迭代的) 其他修正如评论中所述。