首页 > 解决方案 > 编码(输入)在终端中不起作用

问题描述

我正在学习如何使用“输入”功能与数据输入进行交互。我知道这组编码可能在 Sublime Text 中不起作用,但在终端中也不起作用。

我是这样做的 1. 在 Sublime Text 中编写代码 2. 保存文件并通过 Python Launcher 3 再次打开 3. 前 2 行代码没有问题。当我输入一些东西时,程序停止了。4.当我用数字(int)运行类似的程序时没有问题

这些是用 Sublime Text 编写的代码:

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'complete' to end the program. "
message = ""
while message != 'complete':
    message = input(prompt)
    print(message)

这是终端中显示的错误消息:

Lins-MacBook-Pro:~ linyang$ cd '/Users/linyang/Desktop/python_work/' && 
'/usr/bin/pythonw'  '/Users/linyang/Desktop/python_work/parrot.py'  && echo 
Exit status: $? && exit 1

Tell me something, and I will repeat it back to you:
Enter 'complete' to end the program. complete
Traceback (most recent call last):
  File "/Users/linyang/Desktop/python_work/parrot.py", line 5, in <module>
    message = input(prompt)
  File "<string>", line 1, in <module>
NameError: name 'complete' is not defined
Lins-MacBook-Pro:python_work linyang$ 

这是用 Sublime Text 编写的另一组代码:

number = input("May I know how many people will dine? ")
number = int(number)

if number > 8:
    print("Sorry, there is no enough seats.")
else:
    print("Seats available!")

它在终端中完美运行:

cd '/Users/linyang/Desktop/python_work/' && '/usr/bin/pythonw'  
'/Users/linyang/Desktop/python_work/even_or_odd.py'  && echo Exit status: $? 
&& exit 1
Lins-MacBook-Pro:~ linyang$ cd '/Users/linyang/Desktop/python_work/' && 
'/usr/bin/pythonw'  '/Users/linyang/Desktop/python_work/even_or_odd.py'  && 
echo Exit status: $? && exit 1
May I know how many people will dine? 6
Seats available!
Exit status: 0
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

标签: pythonsublimetext3

解决方案


推荐阅读