-2

I'm creating a function (python 2.7) to make a dictionary out of user input with test subject as the key and their test scores as the values, the subjects that the scores will correspond to are provided in a seperate list called subjects so i will require a for loop to cycle through them asking the user for the scores in turn. The function will return the student ID and the scores. I think it's something like this:

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

dict = {}

     for a in subjects:

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

Really struggling, any help appreciated! thanks

Greg Madro
  • 39
  • 1
  • 7

1 Answers1

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

scores = {}

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

subjects must be a list (or any other iterable).
Other fixes as in the comments.

SiHa
  • 7,830
  • 13
  • 34
  • 43
Dschoni
  • 3,714
  • 6
  • 45
  • 80
  • 1
    what's wrong with string formatting ? `raw_input("please enter the score for {} :".format(a))`. And anyway since here `a` is already a string, you don't need to call `str()` on it. – bruno desthuilliers Jan 05 '17 at 14:14
  • Nothing wrong with string formatting. I just wanted to demonstrate the possibility of adding strings together in general. – Dschoni Jan 05 '17 at 14:21
  • Thanks for the tips guys. Also, one final thing, how would I ensure that the user input for the score is a float and is between 0 and 100 for neatness sake. – Samuel Ewen Jan 05 '17 at 14:28
  • 1
    @SamuelEwen why don't you try something by yourself first ? How do you hope to learn anything if you don't do your homework ? Asking for help is ok, but only as long as you _really_ tried by yourself first. – bruno desthuilliers Jan 05 '17 at 14:39
  • http://stackoverflow.com/questions/15099379/limit-input-to-integer-only-text-crashes-python-program – Dschoni Jan 05 '17 at 14:43