Home > other >  I get a syntaxt error when I try to run the program
I get a syntaxt error when I try to run the program

Time:04-05

I get a syntax error on the last else statement. This is supposed to be a grade calculator. If someone enters anything that isn't between 0 and 100, then the program should send a message, and then loop until a valid number is entered. Also I am new to programming, so if there is something else wrong with my code, please let me know!

number = int(input("Enter the numeric grade: "))
if number > 89:
    letter = 'A'
elif number > 79:
    letter = 'B'
elif number > 69:
    letter = 'C'
else:
    letter = 'F'

print("The letter grade is", letter)
number = int(input("Enter the numeric grade: "))
if number > 100:
    print("Error: grade must be between 100 and O")
elif number < 0:
    print("Error: grade must be between 100 and O")
else:
    # The code to compute and print the result goes here
   number = int(input("Enter the numeric grade: "))
if number > 100 or number < 0:
    print("Error: grade must be between 100 and 0")
else:
     # The code to compute and print the result goes here 
    number = int(input("Enter the numeric grade: "))
if number >= 0 and number <= 100:
    else:
        print("Error: grade must be between 100 and O")

CodePudding user response:

Your issue is at the bottom, where you've got an empty if statement followed by an else statement, as well as incorrect indenting. From your code, I believe you are trying to use not.

I would suggest doing one of two things:
1.

if not (number >= 0 and number <= 100):

2.

if number < 0 or number > 100:

These two pieces of code will both produce the same result.

Also, it seems as though you are repeating the same code several times to try to ensure that the user inputs a number between 0 and 100. If so, this can be achieved with a while loop. I've put an example of this below:

number = int(input("Enter the numeric grade: "))
while number < 0 or number > 100:
    print("Error: grade must be between 100 and O")
    number = int(input("Enter the numeric grade: "))

CodePudding user response:

In the last line.

if number >= 0 and number <= 100:
else:
    print("Error: grade must be between 100 and O")

you didn't write anything after the if statement. This code will run

if number >= 0 and number <= 100:
    print('Write something here')
else:
    print("Error: grade must be between 100 and O")

or you can just remove the last if statement its really of no use like try doing something else because there are a lot of if and else statements which don't look nice. try this:

n = int(input())
first = 0
last = 100 
while n < first or n > last:
    print(n,'is not a valid input')
    n = int(input())

CodePudding user response:

From what I've understood, you're trying loop indefinitely until the user input is between 0 and 100.

My solution would be this:

  • Define a function that starts by requesting an input from the user.

  • Use a while loop that will check if the input is 'correct'. In case the input is not correct, the loop will print the error and call back the function again indefinitely till the user's input is between 0 and 100.

  • if the input is between that range, it will then evaluate the grade and return it.

     def yfunc():
         n = int(input('Enter the numeric grade: '))
         while n < 0 or n > 100:
             print("Error: grade must be between 100 and O")
             return yfunc()
         if n > 89:
             letter = 'A'
         elif n > 79:
             letter = 'B'
         elif n > 69:
             letter = 'C'
         else:
             letter = 'F'
         return letter
     yfunc()
    

If you have any questions, feel free to ask.

  • Related