UnboundLocalError: local variable 'input' referenced before assignment
不知道哪里的问题下面是错误 代码
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/pythonProject/osfolderfile/112343455.py", line 100, in <module>
main()
File "C:/Users/Administrator/PycharmProjects/pythonProject/osfolderfile/112343455.py", line 25, in main
insert() #录入学生信息
File "C:/Users/Administrator/PycharmProjects/pythonProject/osfolderfile/112343455.py", line 53, in insert
ffff =input('请输入mtm(如1001):')
UnboundLocalError: local variable 'input' referenced before assignment
"""
---------------------------------
test: rzbbzr
test1: PyCharm
test2: 112343455.py
test3: 2022-02-15 19:49
Pyinstaller -F112343455.py
py转jupyter %load xxxx.py
pip install -i https://pypi.doubanio.com/simple/ 包名
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple +包名
"""
filename='student.txt'
def main():
while True:
menm()
choice=int(input("请选择"))
if choice==0:
answer=input("您确定要退出系统吗?y/n")
if answer=='y' or answer=='Y':
print("谢谢您的使用!!!")
break #退出系统
else:
continue
elif choice==1:
insert() #录入学生信息
elif choice==2:
search()
elif choice==3:
delete()
elif choice==4:
modify()
elif choice==5:
sort()
elif choice==6:
total()
elif choice==7:
show()
def menm():
print("============学生信息管理系统================")
print("------------功能菜单----------------------")
print("\t\t\t\t\t\t1.录入学生信息---------------")
print("\t\t\t\t\t\t2.查找学生信息-----------------")
print("\t\t\t\t\t\t3.删除学生信息---------------")
print("\t\t\t\t\t\t4.修改学生信息----------------")
print("\t\t\t\t\t\t5.排序---------------------")
print("\t\t\t\t\t\t6.统计学生总人数-------------")
print("\t\t\t\t\t\t7.显示所以学生信息------------")
print("\t\t\t\t\t\t0.退出--------------------")
print('-------------------------------------------------------')
def insert():
student_list=[]
while True:
ffff =input('请输入mtm(如1001):')
if not ffff:
break1
name=input('请输入姓名:')
if not name:
break
try:
englist=int(input('请输入英语成绩:'))
python= int(input('请输入python成绩:'))
java= int(input('请输入java成绩:'))
except:
print('输入无效,不是整数类型,请重新输入')
continue
#将录入的学生信息保存到字典中
student={'id':id,'name':name,'english':english,'python':python,'java':java,}
#将学生信息添加到列表中
student_list.append(student)
answer=input=('是否继续添加?y/n\n')
if answer=='y':
continue
else:
break
#调用save()函数
save(student_list)
print('学生信息录入完毕!!!')
def save(lst):
try:
stu_txt=open(filename,'a',encoding='utf-8')
except:
stu_txt = open(filename, 'w', encoding='utf-8')
for item in lst:
stu_txt.write(str(item)+'\n')
stu_txt.close()
def search():
pass
def delete():
pass
def modify():
pass
def sort():
pass
def total():
pass
def show():
pass
if __name__ == '__main__':
main() 本帖最后由 isdkz 于 2022-2-15 21:00 编辑
第71行input后面多了一个等号
=是赋值,导致input成为了一个局部变量,而非内置函数input了,所以你前面用到input函数的时候,解释器就把它当成了局部变量input,局部变量在定义之前使用,自然就报错了
"""
---------------------------------
test: rzbbzr
test1: PyCharm
test2: 112343455.py
test3: 2022-02-15 19:49
Pyinstaller -F112343455.py
py转jupyter %load xxxx.py
pip install -i https://pypi.doubanio.com/simple/ 包名
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple +包名
"""
filename='student.txt'
def main():
while True:
menm()
choice=int(input("请选择"))
if choice==0:
answer=input("您确定要退出系统吗?y/n")
if answer=='y' or answer=='Y':
print("谢谢您的使用!!!")
break #退出系统
else:
continue
elif choice==1:
insert() #录入学生信息
elif choice==2:
search()
elif choice==3:
delete()
elif choice==4:
modify()
elif choice==5:
sort()
elif choice==6:
total()
elif choice==7:
show()
def menm():
print("============学生信息管理系统================")
print("------------功能菜单----------------------")
print("\t\t\t\t\t\t1.录入学生信息---------------")
print("\t\t\t\t\t\t2.查找学生信息-----------------")
print("\t\t\t\t\t\t3.删除学生信息---------------")
print("\t\t\t\t\t\t4.修改学生信息----------------")
print("\t\t\t\t\t\t5.排序---------------------")
print("\t\t\t\t\t\t6.统计学生总人数-------------")
print("\t\t\t\t\t\t7.显示所以学生信息------------")
print("\t\t\t\t\t\t0.退出--------------------")
print('-------------------------------------------------------')
def insert():
student_list=[]
while True:
ffff =input('请输入mtm(如1001):')
if not ffff:
break # 这里break多了一个1
name=input('请输入姓名:')
if not name:
break
try:
english=int(input('请输入英语成绩:')) # 这里把english写成了englist
python= int(input('请输入python成绩:'))
java= int(input('请输入java成绩:'))
except:
print('输入无效,不是整数类型,请重新输入')
continue
#将录入的学生信息保存到字典中
student={'id':ffff,'name':name,'english':english,'python':python,'java':java,} # 这里id改成ffff,因为你前面定义的变量名是ffff,你这里的id会被当成内置函数id()
#将学生信息添加到列表中
student_list.append(student)
answer=input('是否继续添加?y/n\n') # 这里把=去掉
if answer=='y':
continue
else:
save(student_list)
print('学生信息录入完毕!!!')
break
#调用save()函数
# save(student_list) # 把这两句放到else语句块里面,因为else里面break了,如果不放到else语句块里,这两句将没有执行的机会
# print('学生信息录入完毕!!!')
def save(lst):
try:
stu_txt=open(filename,'a',encoding='utf-8')
except:
stu_txt = open(filename, 'w', encoding='utf-8')
for item in lst:
stu_txt.write(str(item)+'\n')
stu_txt.close()
def search():
pass
def delete():
pass
def modify():
pass
def sort():
pass
def total():
pass
def show():
pass
if __name__ == '__main__':
main() 本帖最后由 ckblt 于 2022-2-15 20:16 编辑
"""
---------------------------------
test: rzbbzr
test1: PyCharm
test2: 112343455.py
test3: 2022-02-15 19:49
Pyinstaller -F112343455.py
py转jupyter %load xxxx.py
pip install -i https://pypi.doubanio.com/simple/ 包名
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple +包名
"""
filename = "student.txt"
def main():
while True:
menm()
choice = int(input("请选择"))
if choice == 0:
answer = input("您确定要退出系统吗?y/n")
if answer == "y" or answer == "Y":
print("谢谢您的使用!!!")
break# 退出系统
else:
continue
elif choice == 1:
insert()# 录入学生信息
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
total()
elif choice == 7:
show()
def menm():
print("============学生信息管理系统================")
print("------------功能菜单----------------------")
print("\t\t\t\t\t\t1.录入学生信息---------------")
print("\t\t\t\t\t\t2.查找学生信息-----------------")
print("\t\t\t\t\t\t3.删除学生信息---------------")
print("\t\t\t\t\t\t4.修改学生信息----------------")
print("\t\t\t\t\t\t5.排序---------------------")
print("\t\t\t\t\t\t6.统计学生总人数-------------")
print("\t\t\t\t\t\t7.显示所以学生信息------------")
print("\t\t\t\t\t\t0.退出--------------------")
print("-------------------------------------------------------")
def insert():
student_list = []
while True:
ffff = input("请输入mtm(如1001):")
if not ffff:
break
name = input("请输入姓名:")
if not name:
break
try:
english = int(input("请输入英语成绩:"))# 单词拼写要正确
python = int(input("请输入python成绩:"))
java = int(input("请输入java成绩:"))
except:
print("输入无效,不是整数类型,请重新输入")
continue
# 将录入的学生信息保存到字典中
student = {
"id": id,
"name": name,
"english": english,
"python": python,
"java": java,
}
# 将学生信息添加到列表中
student_list.append(student)
answer = input("是否继续添加?y/n\n")
if answer == "y":
continue
else:
break
# 调用save()函数
save(student_list)
print("学生信息录入完毕!!!")
def save(lst):
try:
stu_txt = open(filename, "a", encoding="utf-8")
except:
stu_txt = open(filename, "w", encoding="utf-8")
for item in lst:
stu_txt.write(str(item) + "\n")
stu_txt.close()
def search():
pass
def delete():
pass
def modify():
pass
def sort():
pass
def total():
pass
def show():
pass
if __name__ == "__main__":
main() isdkz 发表于 2022-2-15 20:06
第71行input后面多了一个等号
=是赋值,导致input成为了一个局部变量,而非内置函数input了,所以你前面 ...
为啥提示53行,我去,找半天 本帖最后由 isdkz 于 2022-2-15 20:57 编辑
swanseabrian 发表于 2022-2-15 20:48
为啥提示53行,我去,找半天
因为你第71行并不会引发错误,但是你第71行会导致第53行出现错误
页:
[1]