当Input()返回错误时,如何在Python 3中获取用户输入

2024-10-04 05:33:55 发布

您现在位置:Python中文网/ 问答频道 /正文

  • 在Python3中,当我使用input()时会出现错误,我希望获取用户输入并对其进行处理
  • 我正在BBEdit、Sublime或IDLE中执行python脚本
  • 除非删除Input()语法,否则代码将导致错误:

input("Hi I'm new to python")

userInput = input("Enter a string")
print(f"You entered {userInput}")

在控制台中,它显示来自输入的字符串(“字符串”),但任何用户击键都会在代码编辑器中键入,而不是以交互方式键入

我读到Python不能通过这些应用程序进行交互,但我不知道如何执行语法正确的“输入”(“输入你最喜欢的寿司卷”)并与之交互(在pc和Mac上)

从控制台:

Enter a string
Traceback (most recent call last):
File "/Users/michaelking/Desktop/BBEditRunTemp-hellowWorld.py", line 1, in userInput = input("Enter a string")
EOFError: EOF when reading a line
================================================================================ Nov 2, 2020 at 9:53:07 PM ~/Desktop/hellowWorld.py Traceback (most recent call last): File "/Users/michaelking/Desktop/BBEditRunTemp-hellowWorld.py", line 1, in userInput = input() EOFError: EOF when reading a line


Tags: 字符串代码用户pyinputstring键入错误
2条回答
userInput = input("Enter a string")
print("You entered {}".format(userInput))

当您运行一个需要交互式输入的脚本时,您必须在终端(或类似于iTerm的终端)中运行它

我无法与其他产品交流:-)但是当使用BBEdit时,#上的“Run-in-Terminal”命令!菜单会帮你的

相关问题 更多 >