使用Python和SQL时获取NamError

时间:2019-02-26 22:40:50

标签: python mysql sql sqlite nameerror

我正在运行一个学生数据库,并在pycharm上使用python 2.7。这是脚本

FirstName = input("Enter the Student's first name")
    if not FirstName or type(FirstName) != str:
    print("Enter a real name silly")
    exit()

和创建表的语句看起来像这样

drop table if exists StudentID;
drop table if exists FirstName;
drop table if exists LastName;
drop table if exists GPA;
drop table if exists Major;
drop table if exists FacultyAdvisor;
CREATE TABLE Student(
  StudentID int PRIMARY KEY AUTOINCREMENT ,
  FirstName varchar(25),
  LastName varchar(25),
  GPA NUMERIC,
  Major varchar(10),
  FacultyAdvisor varchar(25)
)

我得到的错误是

FirstName = input("Enter the Student's first name")
  File "<string>", line 1, in <module>
NameError: name 'john' is not defined

1 个答案:

答案 0 :(得分:1)

我的猜测是您正在使用Python2

接收来自用户的String输入时,您需要使用raw_input()方法:

FirstName = raw_input("Enter the Student's first name")