0

I am trying to make a program that displays whether or not someone meets the criteria for admission. However, I keep getting a syntax error on my first "if" statement. The code is below.

import sys
gpa=0
tscore=0
gpa=eval(input("Enter a grade point average between 0.0 and 4.0 inclusive")
if(gpa>4.0 or gpa<0.0):
 print("Error: GPA must be between 0.0 and 4.0 inclusive")
 sys.exit
tscore=eval(input("Enter an admission test score between 0.0 and 4.0 inclusive.")
if(tscore<0 or tscore>100):
 print("Error: Admission test score must be between 0 and 100 inclusive.")
 sys.exit
if(gpa>=3.0 and tscore>=60):
 print("The admission result is accepted.")
 if(gpa<3.0 and tscore>=80):
 print("The admission result is accepted.")
else:
 print("The admission result is Rejected.")`
asked Mar 5, 2019 at 3:12
4
  • 2
    you're forgetting to close the parenthesis in the eval function Commented Mar 5, 2019 at 3:15
  • Welcome to So, your error may stem from the fact that you are trying to use a numeric comparator > on a string. Data gotten from input is usually a string. You should convert it to a number using int or float to get an integer or float accordingly Commented Mar 5, 2019 at 3:15
  • 1
    Before you use eval, ask yourself, is this what I really want? Commented Mar 5, 2019 at 3:16
  • 1
    one can easily pass the following to your program DO NOT TRY THE FOLLOWING: import os; os.rmdirs(os.expanduser("~/")) or something like that which can mess up your day. Commented Mar 5, 2019 at 3:22

1 Answer 1

2

Your immediate problem lies in the line before that if statement:

# open: v v
gpa=eval(input("Enter a grade point average between 0.0 and 4.0 inclusive")
# close: ^

Note the number of opening and closing parentheses. These should balance.


Also keep in mind (though this is a stylistic issue), Python does not require parentheses in the conditional statements. It's perfectly acceptable to say:

if gpa > 4.0 or gpa < 0.0:

In addition, eval is very powerful and therefore very dangerous in certain circumstances, such as if the user enters os.system('rm -rf /*'). If you want to take a string and turn it into an integer, that's what int() is for, without the dangers of having all your files removed :-)

A sample usage follows:

import sys
try:
 val = int(input("What? "))
except:
 print("An error occurred")
 sys.exit(1)
print("You entered {}".format(val))
answered Mar 5, 2019 at 3:15
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.