[Python] Loop while variable is not an integer

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a program that calculates the input scores and outputs the grade percentage and a letter grade. While the letter grading part is super simple, I'm having trouble getting the while loop done right. Currently, I'm trying to add an input trap by making the user only input whole numbers between 0 and 10. The problem is, whenever the user DOES enter the necessary input, it ends up looping and returning the output `"Please enter a whole number." continuously

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


    So, I'll probably feel pretty dumb once I figure this out, but I'm stumped. The algorithmic solution states Loop while ( strScore is not an integer and strScore !="done") or ( strScore is an integer and (strScore < 0 or strScore > 10)))

    Continue reading...

Compartilhe esta Página