0

I've been avoiding using boolean flags for a while now because I can't ever seem to make them work, and I can usually accomplish the task using other means. But now I have to and it's giving me trouble.

This program asks a user to input student scores, and then appends them to a list. When the user is done (indicated by inputting 'n' or 'N'), the program should output the list of scores and the average score. But try (and search) as I might, I can't get it to work properly.

scores = []

stop = False
while not stop:
    score = int(input('Enter a score: '))
    scores.append(score)
    another = input('Another (y/n)?: ')
    if another == 'y' or 'Y':
        stop = False
    elif another == 'n' or 'N':
        stop = True

avg = sum(scores) / len(scores)

print(scores)
print(f'Avg Score: {avg}')

I've watched videos and read SO, and my code looks to me exactly the same as some of the examples I've seen. But the loop won't end when the user inputs 'n' or 'N' as I hoped it would. Any tips on boolean flags in general? What am I doing wrong?

  • 1
    The problem has nothing to do with the boolean flag. Your `if` conditions don't do what you think they do. – Barmar Feb 17 '22 at 17:46
  • 1
    Why do you think you need the flag? Just use `break` instead of `stop = True` – Barmar Feb 17 '22 at 17:47

0 Answers0