0

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)))

2
  • 3
    You definitely should check out PEP 8, the Python style guide. Commented Nov 11, 2016 at 17:14
  • 1
    1) input value is always a string. 2) use type(strScore) if you want to compare types. Commented Nov 11, 2016 at 17:18

2 Answers 2

3

strScore != int doesn't test if the value is an integer; it checks if the value equal to the int type. You want not isinstance(strScore, int) in this case.

However, you should try to avoid making direct type checks. The important thing is that a value behaves like an float.

print("Enter the homework scores one at a time. Type \"done\" when finished.")
hwCount = 1 
while True:
    strScore = input(f"HW#{hwCount} score: ")
    if strScore == "done":
        break
    try:
        score = float(strScore)
    except ValueError:
        print(f"\"{strScore}\" is not a valid score, please try again.")
        continue

    if not (0 <= score <= 10):
        print("Please enter a value between 0 and 10.")
        continue

    # Work with the validated value of score
    # ...
    hwCount += 1
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help with this! Is there a way I could write this that does not use the While True format?
That's a common Python idiom. It's often cleaner to assume a loop is infinite, breaking when appropriate, than it is to try to fit all the logic in the exit condition. It also helps when you have code (like input("HW#1 score: ")) that is supposed to execute at least once, guaranteeing that you will enter the loop.
@SosigRamsey you can avoid the while True: and the break with while (strScore := input(f"HW#{hwCount} score: ")) != "done": but that looks super-confusing.
0

In order to meet both the question and Sosig Ramsey's further request:

# Change the string value of the EXIT_CODE constant below to whatever word
# that you want the user to use in order to exit the 'while' loop:
EXIT_CODE="done"

print(f"Enter the homework scores, one at a time. When finished, type \"{EXIT_CODE}\" and press Enter.")
hwCount = 1
# Use 'repeat' as a loop control variable:
repeat=True
while repeat:
    strScore = input(f"HW#{hwCount} score: ")
    # Compares lowercased 'strScore' with lowercased 'EXIT_CODE':
    if strScore.lower() == EXIT_CODE.lower():
        repeat=False
        continue
    try:
        strScore = float(strScore)
    # What to do if converting strScore's value to float returns an error
    # because it tried to convert string to float:
    except ValueError:
        print(f"\"{strScore}\" is not a valid score. Please, try again.")
        continue
    if not (0 <= strScore <= 10):
        print("Please, provide a number between 0 and 10 (inclusive).")
        continue
    # Work with the validated value of score
    # ...
    hwCount += 1

As of 2024-09-22, I'm a Python beginner student. Using didactic text and adding comments within the code has helped me a lot. Hopefully, the comments above are going to help those who, like me, are still in the beginning and need some context-based explanation in order to fully understand why the code was built like this. The code above was tested (and worked) with Python version 3.12.5 on Visual Studio Code version 1.93.1.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.