1

Hello I have problem looping when printing data in a list.

def inputScore(scor) :
    for i in range(len(scor)) :
        scor[i] = int(input("Enter Score Number : "))

def Display(scr) :
    for i in scr:
        if i >= 86 and i <= 100 :
            x = "A"
        elif i >= 70 and i < 86 :
            x = "B"
        elif i >= 60 and i < 70 :
            x = "C"
        elif i >= 50 and i < 60 :
            x = "D"
        elif i >= 0 and i < 50 :
            x = "E"
        else :
            print("Invalid Score")
        for i in range(0,3) : # If I remove this code "for..", it will error "IndexError: list index out of range"
            print("Score Number",scr[i],"Letter Grades", x)

def main () :
    scor = [int]*3
    inputScore(scor)
    Display(scor)
main()

Example:

# Input :
85
60
40

# Output that I want :
Score Number 85 Letter Grades A
Score Number 60 Letter Grades C
Score Number 40 Letter Grades E

# Output that I got :
Score Number 85 Letter Grades A
Score Number 60 Letter Grades A
Score Number 40 Letter Grades A

Score Number 85 Letter Grades C
Score Number 60 Letter Grades C
Score Number 40 Letter Grades C

Score Number 85 Letter Grades E
Score Number 60 Letter Grades E
Score Number 40 Letter Grades E

There are 3 looping for Letter Grades (A = 3 times, C = 3 times and E = 3 times), I tried to give for i in range(0,3) for stop looping but it doesn't work, Letter Grades always prints 9 times with 3 A, 3 C, and 3 E. How to solve it and make it output like in the example above?

1
  • It’s good time to try debug this program yourself to learn the logic - pythontutor.com. Commented Jan 3, 2021 at 3:40

1 Answer 1

1

The inner for loop is unnecessary. The outer for loop already iterates through the scores. In each iteration, i is a score, not an index -- to get an index, you use range. Therefore, there's no need to index the scr list at all -- you would print i itself rather than scr[i].

Also, with the current code, the grade for 85 would be B rather than A. Perhaps you need to adjust the bounds for the A and B grades.

Another issue is that for an invalid score, it would still attempt the final print. This would fail if an invalid score occurred as the first score (since x wouldn't be defined). If an invalid score occurred as a subsequent score, the print would show you the grade for the previous score, which you don't want. You can get around this by setting x to be the empty string at the start of each iteration and checking if it has a non-empty value before doing the final print of the grade.

The following code resolves the issues discussed:

def inputScore(scor):
    for i in range(len(scor)):
        scor[i] = int(input("Enter Score Number: "))

def display(scr):
    for i in scr:
        x = ""
        if i >= 85 and i <= 100:
            x = "A"
        elif i >= 70 and i < 85:
            x = "B"
        elif i >= 60 and i < 70:
            x = "C"
        elif i >= 50 and i < 60:
            x = "D"
        elif i >= 0 and i < 50:
            x = "E"
        else:
            print("Invalid Score")
        if x:
            print("Score Number:", i, "Letter Grade:", x)

def main():
    scor = [int] * 3
    inputScore(scor)
    display(scor)

main()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.