我正在学习如何使用"输入"与数据输入交互的功能。我知道这组编码可能在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]