我试图让用户输入数据来执行插入,只要我有数字,它就能工作,但是当我输入字母时,它给了我错误"LettersUsed“的定义。我尝试将输入转换为str(输入(“任何”)),但这并没有任何帮助,为什么要这样做?
import pymongo
import sys
#get a connection to database
connection = pymongo.MongoClient('mongodb://localhost')
#get a handle to database
db=connection.test
vehicles=db.vehicles
vehicle_VIN = input('What is the Vehicle VIN number? ')
vehicle_Make = input('What is the Vehicle Make? ')
newVehicle = {'VIN' : (vehicle_VIN).upper(), 'Make' : (vehicle_Make)}
try:
vehicles.insert_one(newVehicle)
print ('New Vehicle Inserted')
except Exception as e:
print 'Unexpected error:', type(e), e
#print Results
results = vehicles.find()
print()
# display documents in collection
for record in results:
print(record['VIN'] + ',',record['Make'])
#close the connection to MongoDB
connection.close()发布于 2015-06-11 04:57:30
消息:未定义名称“DAFEQF”
在Python2中,代码input()等于eval(raw_input(prompt))。这意味着无论您输入什么输入,Python2都会尝试“评估”该输入,并会抱怨您的输入没有定义。
确保将input()替换为raw_input() (这仅用于Python2!)
替换
vehicle_VIN = input('What is the Vehicle VIN number? ')
vehicle_Make = input('What is the Vehicle Make? ')使用
vehicle_VIN = raw_input('What is the Vehicle VIN number? ')
vehicle_Make = raw_input('What is the Vehicle Make? ')https://stackoverflow.com/questions/30771488
复制相似问题