【python 报错篇】python 2.7 使用input函数时报错

python 2.7 使用input函数时,直接按下enter键(回车键)会报错

    while True:
        export_ornot = input("please input 'y' export or not\n")
        if export_ornot:
            break  # 如果输入非空,退出循环
        else:
            print("NULL,Please input again")

Traceback (most recent call last):
  File "C:\Users\code\PycharmProjects\csvtoxml\main.py", line 13, in
    export_ornot = input("please input 'y' export or not\n")
  File "", line 0
    
    ^
SyntaxError: unexpected EOF while parsing

这是因为在 Python 2 中,使用 raw_input() 函数来获取用户输入。input() 函数在 Python 2 中会尝试执行输入的代码,这在某些情况下可能导致错误。

将input()函数修改为raw_input(),报错解决。

正确代码:

    while True:
        export_ornot = raw_input("please input 'y' export or not\n")
        if export_ornot:
            break  # 如果输入非空,退出循环
        else:
            print("NULL,Please input again")

补充:

使用raw_input()函数输入字符串时不需要带引号

使用input()函数输入字符串时需要带引号

你可能感兴趣的:(python)