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.")`
1 Answer 1
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))
evalfunction>on a string. Data gotten frominputis usually a string. You should convert it to a number usingintorfloatto get an integer or float accordinglyimport os; os.rmdirs(os.expanduser("~/"))or something like that which can mess up your day.