鱼C论坛

 找回密码
 立即注册
查看: 1923|回复: 5

求助python yes or no 的选择问题

[复制链接]
发表于 2018-11-20 22:36:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
python 代码:

    numsExit = []
    inputExit = eval(input("Y退出,N继续:"))
    numsExit.append(eval(inputExit))
    if (numsExit[0] == y) or (numsExit[0] == y):
        break
    else:
        continue
报错:
Traceback (most recent call last):
  File "D:\PYECourse\CalStatisticsV1.py", line 45, in <module>
    inputExit = eval(input("Y退出,N继续:"))
  File "<string>", line 1, in <module>
NameError: name 'y' is not defined
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-20 22:39:49 | 显示全部楼层
y要加''
即:
  1. if (numsExit[0] == 'y') or (numsExit[0] == 'y'):
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-20 22:41:35 | 显示全部楼层
我试过:

Traceback (most recent call last):
  File "D:\PYECourse\CalStatisticsV1.py", line 45, in <module>
    inputExit = eval(input("Y退出,N继续:"))
  File "<string>", line 1, in <module>
NameError: name 'y' is not defined
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-20 22:42:41 | 显示全部楼层
while True:
    main()
   
    numsExit = []
    inputExit = eval(input("Y退出,N继续:"))
    numsExit.append(eval(inputExit))
    if (numsExit[0] == 'y') or (numsExit[0] == 'Y'):
        sys.exit()
        #break
    else:
        continue
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-20 22:45:43 | 显示全部楼层
你这里根本没必要用eval
执行eval的结果是将你输入的字符串‘y’识别成变量
你之前还没定义过
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-20 22:55:04 | 显示全部楼层
while True:
    main()
   
    numsExit = []
    inputExit = (input("Y退出,N继续:"))
    numsExit.append(eval(inputExit))
    if (numsExit[0] == 'y') or (numsExit[0] == 'Y'):
        sys.exit()
        #break
    else:
        continue

Y退出,N继续:y
Traceback (most recent call last):
  File "D:\PYECourse\CalStatisticsV1.py", line 46, in <module>
    numsExit.append(eval(inputExit))
  File "<string>", line 1, in <module>
NameError: name 'y' is not defined
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-2 05:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表