Python Forum

Full Version: Help converting int to str value in loop statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good evening

The issue i'm trying to solve with this program is I need to convert the code on line 23 to a str value "end" in order to display the average scores.

Thank you very much in advance.
#!/usr/bin/env python3

# display a welcome message
print("The Test Scores program")
print()
print("Enter test scores")
print("Enter 'end' to end input")
print("====================")
# get scores from the user

# initialize the variable for accumulating scores
counter = 0
score_total = 0
test_score = 0

choice = "y"
while choice.lower() == "y":
    test_score = int(input("Enter test score: "))
    if test_score >= 0 and test_score <= 100:
        score_total += test_score
        counter += 1
    
    elif test_score == 999:
        average_score = round(score_total / counter)
        print("======================")
        print("Total Score:  ", score_total,
      "\nAverage Score:", average_score)
        print()
        choice = input("Enter another set of test scores (y/n):")
        if choice =="n":
            break
        print()
        print("Enter test scores")
        print("Enter 'end' to end input")
        print("====================")
        counter = 0
        score_total = 0
        test_score = 0
        continue
    else:
        print("test score must be from 0 through 100. Try again.")
        
print("Bye")
read the input as a string, first, and check for "end". then if it might be a number, convert to whatever you need to do your calculation.
Thanks for the help. Here is the result.
#!/usr/bin/env python3

# display a welcome message
print("The Test Scores program")
print()
print("Enter test scores")
print("Enter 'end' to end input")
print("====================")
# get scores from the user

# initialize the variable for accumulating scores
counter = 0
score_total = 0
test_score = 0
average_score = 0
choice = "y"
while choice.lower() == "y":
    test_score = str(input("Enter test score: "))
    if test_score == "end":
        print("=====================")
        print("Total Score: ", score_total,
              "\nAverage Score: ", average_score)
        print()
        choice = input("Enter another set of test scores (y/n):")
        if choice =="y":
            counter = 0
            score_total = 0
            test_score = 0
            average_score = 0
            continue
        elif choice =="n":
            break
    test_score = int(test_score)
    if test_score >= 0 and test_score <=100:
        score_total += test_score
        counter += 1
        average_score = round(score_total / counter)
        continue
    else:
        print("Test score must be from 0 through 100. Try again.")

print("Bye")