user8310358
user8310358

Reputation:

using %s, %d to assign pass/fail to grade determination based on user input score (python)

desired outcome: Enter a test score (-99 to exit): 55 Enter a test score (-99 to exit): 77 Enter a test score (-99 to exit): 88 Enter a test score (-99 to exit): 24 Enter a test score (-99 to exit): 45 Enter a test score (-99 to exit): -99 55 77 88 24 45 P P P F F

Process finished with exit code 0

The code so far: (works except for Pass fail assignment)

Python Program to ask user to input scores that are added to a list called scores. Then prints under that score P for Pass F for fail.

scores = [] #list is initialized

while True:
    score = int(input("Enter a test score (-99 to exit): "))
    if score == -99:
        break
    scores.append(score)

def print_scores(): #accepts the list and prints each score separated by a space
    for item in scores:
        print(item, end = " ")      # or 'print item,'
print_scores()       # print output

def set_grades():       #function determines whether pass or fail
    for grade in scores:
        if score >= 50:
            print("P")
        else:
            print("F")
print(set_grades)

Upvotes: 0

Views: 412

Answers (1)

M Palmer
M Palmer

Reputation: 181

You're thinking along the right lines, but you need to run through your program from the top and make sure that you're getting your reasoning right.

Firstly, you've written the program to print out all the scores before you get to checking if they're passes, so you'll get a list of numbers and then a list of P/F. These need to happen together to display properly. Also, make sure that you keep track of what variable is what; in your last function, you attempt to use 'score', which doesn't exist anymore. Finally, I'm not sure what exactly you're asking with %d or %s, but you may be looking for named arguments with format(), which are shown below.

scores = [] #list is initialized

while True:
    score = int(input("Enter a test score (-99 to exit): "))
    if score == -99:
        break
    scores.append(score)

for item in scores:
    if item >= 50:
        mark = 'P'
    else:
        mark = 'F'

    print('{0} {1}'.format(item, mark))

I believe this is what you're looking for.

Upvotes: 1

Related Questions