Pergunta

I'm still pretty new to python, and I have to write this program where the user enters homework grades, types 'done' and gets their grades back. I'm almost done with it, but I keep getting error messages when it gets to the loop:

TypeError: unorderable types: str() > int().

print("Enter the homework scores one at a time. Type 'done' when done")
hwCount = 1
totalScore = 0
strScore = input("Enter the HW#" + str(hwCount) + " score: ")
while strScore != "done":
    if ( strScore is int and strScore >= 0 and strScore <= 10 ):
        totalScore = totalScore + strScore
        hwCount = hwCount + 1
    elif ( strScore is int and strScore < 0 or strScore > 10):
    print("Please enter a number between 0 and 10")
    else:
        print("Please enter only whole numbers")
    strScore = input("Enter HW#" + str(hwCount) + " score: ")

I tried this:

strScore = int(input("Enter the HW#" + str(hwCount) + " score: ")

but it would only print the else statement, and then I got the same error as before. If someone could help me figure this out I'd really appreciate it

Foi útil?

Solução

strScore is a string. Your while loop should look like this:

while strScore != "done":
    try:
        score = int(strScore)
        if score >= 0 and score <= 10:
            totalScore = totalScore + score
            hwCount = hwCount + 1
        else:
            print("Please enter a number between 0 and 10")
    except ValueError:
        print("Please enter only whole numbers or 'done'")
    strScore = input("Enter HW#" + str(hwCount) + " score: ")

As you show in your code, there are three cases to handle - user enters a valid score, user enters a valid number but invalid score, and user enters an invalid number. If the user enters an invalid integer, trying int(strScore) will raise a ValueError, which we can catch and report. Knowing that otherwise score will be a valid int, we only have to check if it is a valid score or not, allowing you to change the elif into a simple else.

Outras dicas

strScore = int(input("Enter the HW#" + str(hwCount) + " score: ")

won't work because you have

while strScore != 'done':

which requires strScore to be a string (eventually). See the Rob Watts' answer for the code (beaten me to it)

input (python3) always returns a str. You need to convert it for integer comparison and adding. I like to break my valid input logic from my other work, so if you define function like this(hopefully reusable):

def get_valid_input(prompt, min_score=0, max_score=10):
   while True:
        text = input(prompt).strip()
        if text == 'done':
            return text
        try:
            score = int(text)
        except ValueError:
            print("Please enter a number or done")
            continue
        if min_score <= score <= max_score:
            return score
        else:
            print("Please enter a number between %s and %s" % (min_score, max_score))

then to get your HW scores, its a simple matter

total_score = 0
hw_count = 1
score = get_valid_input("Enter HW#%s score: " % hw_count)
while score != "done":
    total_score += score
    hw_count += 1
    score = get_valid_input("Enter HW#%s score: " % hw_count)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top