0

I have the following code:

def input_scores():
scores = []
y = 1
for num in range(5):
    score = int(input(print('Please enter your score for test %d: ' %y)))

    while score < 0 or score > 100:
        print ('Error --- all test scores must be between 0 and 100 points')
        score = int(input('Please try again: '))
    scores.append(score)
    y += 1
    return scores   

When I run it, the output is as follows:

Please enter your score for test 1: 
None

Then I'll enter the test score next to None, as, say 95 It then runs through the rest of the program without prompting me for the next test score to add to the scores list. I'm really curious why that is

Thanks in advance for taking the time to help

Sincerely, ~Dustin

Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
Dustin Burns
  • 195
  • 2
  • 4
  • 9
  • 4
    Just a quick note: That `input` function call is wrong, you don't need the `print` function inside it. It should just be the string. That's why it prints `None` after the prompt. – Some programmer dude Apr 02 '12 at 01:51
  • 1
    Your function needs to be indented (everything after first line) – SirGuy Apr 02 '12 at 01:55
  • Another quick note: I infer from you code that you are using Python 3, not Python 2 - it's important to tag your questions as Python-3.x so that we don't get confused. We usually assume you're using Python 2 unless you say otherwise, and the advice we need to give may be quite different. – Li-aung Yip Apr 02 '12 at 01:55
  • And you may be able to get rid of `y` variable. If you ever need it, you can just use `len(scores) + 1`. – max Apr 02 '12 at 01:58

5 Answers5

5

You return from inside the loop. Move return scores one indent left.

max
  • 49,282
  • 56
  • 208
  • 355
2

your return statement is indented too much, causing the function to return on the first iteration. It needs to be outside of the for block. This code works:

def input_scores():
    scores = []
    y = 1
    for num in range(5):
        score = int(input('Please enter your score for test %d: ' %y))
        while score < 0 or score > 100:
            print ('Error --- all test scores must be between 0 and 100 points')
            score = int(input('Please try again: '))
        scores.append(score)
        y += 1
    return scores
Jacob Stoner
  • 1,113
  • 9
  • 10
0

You indentation of the code seems whacky. It looks like the return statement is inside the scope of the for loop. So after the first iteration the return statement takes you out of the function completely.

Amjith
  • 22,626
  • 14
  • 43
  • 38
0

You're returning scores at the end of each loop iteration (so in other words, after the first loop iteration finishes, you return all the scores thereby exiting the function, and the loop).

Change your code to be:

for num in range(5):
    # ...
return scores    # Note the indentation is one tab less than the loop's contents
Cameron
  • 96,106
  • 25
  • 196
  • 225
0

Others have correctly pointed out that the indentation of your return statement was causing the problem. Also, you might want to try it like this, using len(scores) to control the loop, as @max suggested:

def input_scores(num_tests=5, max=100, min=0):
    scores = []
    while len(scores) < num_tests:
        score = int(input('Please enter your score for test {0}: '.format(len(scores)+1)))
        if score < min or score > max: 
            print ('Error --- all test scores must be between 0 and 100 points.')
        else:
            scores.append(score)
    return scores
alan
  • 4,752
  • 21
  • 30