python - 编码(输入)在终端中不起作用
问题描述
我正在学习如何使用“输入”功能与数据输入进行交互。我知道这组编码可能在 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]
解决方案
推荐阅读
- c - 如何让 fread() 从内存中读取数据?
- c++ - 排序 an 在用户定义的函数中产生问题
- android - 将使用 SHA-1 密钥的 android 项目转移到另一台电脑
- python - Python Selenium - 获取具有日期角色名的元素
- jenkins-pipeline - 在 Jenkins 中将 DSL 作业转换为管道
- laravel - 带有两个参数的 URL
- python - Python“graphics.py”库工作,但没有方法 .yUp()
- r - 根据附加条件构建变量滞后
- java - 使用 iText 7 和 Java 生成 pdf 不能换行长英文单词
- javascript - 反应本机应用程序构建成功,但没有显示