You need to enable JavaScript to run this app. Python编写菜单程序报NameError: raw_input未定义错误如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

Python编写菜单程序报NameError: raw_input未定义错误如何解决?

报错原因
  • raw_input() 是 Python 2 版本独有的控制台输入函数,Python 3 版本已经将该功能合并到 input() 函数中,直接移除了 raw_input() 的内置定义,你当前运行代码使用的是 Python 3 环境,因此触发该命名不存在的报错。
修复方案

将代码中调用 raw_input() 的行替换为 input() 即可正常运行,修复后完整代码如下:

ans=True
while ans:
    print("""
    1.Add a Student
    2.Delete a Student
    3.Look Up Student Record
    4.Exit/Quit
    """)
    ans=input("What would you like to do? ")
    if ans=="1":
      print("\nStudent Added")
    elif ans=="2":
      print("\n Student Deleted")
    elif ans=="3":
      print("\n Student Record Found")
    elif ans=="4":
      print("\n Goodbye") 
      ans = None
    else:
       print("\n Not Valid Choice Try again")

内容的提问来源于stack exchange,提问作者Xghost12S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 03:15:00