Prompting an error message when user inputs a neagtive number

score = input(“Enter Score :”)
try:
fscore = float(score)
except :
print(“Error, enter numerical values”)
quit()
if fscore > 1.0:
print(“Error, not in range”)
elif fscore < 0:
print(“Error, not in range”)
elif fscore >= 0.9:
print (“A”)
elif fscore >= 0.8 :
print (“B”)
elif fscore >= 0.7:
print (“C”)
elif fscore >= 0.6:
print (“D”)
else:
print(“F”)

The above code is supposed to take input from range 0.0 to 1.0.An error appears when I enter a negative number "‘-0.025’ is not recognized as an internal or external command,

operable program or batch file" why is that ? how do I rectify this ?

hi I have read your problem, It will take time to fix but you can fix this, no worries, OK!.
at first the line of elif fscore < 0, focus on this line less than zero means no negative numbers are required try to engage here and other elifs are lengthy and might cause an error in case of fscore >= 0.9 prints A but the other says if >=0.8 print B so if you enter 0.9 it will also print B with A, manage the elif syntax and try to create a function out of it.
In the meantime I am writing a code similar to yours so I can help In more details, But kindly try. if you have any questions ask me!

See if this will help. I changed your code a little.

try: # Try block to handle any input not a float
     score = float(input('Enter a score: '))
except ValueError:
    print('error')

else:
    if score > 1.0 or score < 0: # If the input is greater than 1 or less than 0 throw error
        print('error')
    elif score >= 0.9: # The rest should be self explanatory
        print('A')
    elif score >= 0.8:
        print('B')
    elif score >= 0.7:
        print('C')
    elif score >= 0.6:
        print('D')
    else:
        print('F')

One thing to also keep in mind is you can combine some of these… like if you want to check if a number is between other numbers you can do something like:

x = 10

if 5 < x < 10:    # reads like 5 is less than x which is less than 10
    print(x,"is in between 5 and 10")
else: 
   print(x,"is not between 5 and 10")

Regarding the original script, I pasted it into my environment and it did not give those errors with a negative number, it worked as expected. I don’t see anything wrong with the code, but your code didn’t include the indents, so maybe you have something indented incorrectly. Based on the error it almost seems like you’re accidentally entering the number into your OS prompt, not this script input prompt or something.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.