Python Forum
Help converting int to str value in loop statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help converting int to str value in loop statement
#1
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")
Reply
#2
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#3
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While loop and If Statement farzankh 3 3,215 Jan-27-2019, 10:13 PM
Last Post: stullis
  if statement in for loop Danielk121 3 3,696 Nov-13-2017, 01:52 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020