Python代码不能运行[输入]

问题描述:

我是一个完整的noob在python和编程,所以我敢肯定这个问题可能会刺激一些,所以我正在阅读这本书,它有这个例子,它不有用完raw_inputPython代码不能运行[输入]

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
# Filename: continue.py 
#!/usr/bin/python 
# Filename: break.py 
while True: 
s = (raw_input('Enter something : ')) 
if s == 'quit': 
break 
print('Length of the string is', len(s)) 
print('Done') 

我的意思是,当我在书里只加入input像代码不能运行。这是为什么?

+0

你是什么意思*“add just'input'”*?哪里?哪个版本的Python(3.x或2.x)?这是你真正想要使用的缩进吗? – jonrsharpe 2014-10-03 13:19:03

+1

你的python版本是什么? 'input()'用于python 3.x ...在终端运行这个命令'python --version'!你也有不好的缩进! – Kasramvd 2014-10-03 13:19:08

如果本书使用Python 2.x,则只有raw_input可以工作。如果你使用的是Python 3.x,使用input()将会很好。

在Python中的缩进是非常重要的,它定义代码块(在许多其他语言,如C/C++/JAVA /压痕等替代{和} ...)

尝试重新缩进你的代码正确。

此代码运行正常:

while True: 
    s = (raw_input('Enter something : ')) 
    if s == 'quit': 
     break 
print('Length of the string is', len(s)) 
print('Done') 

另注:不直接从文本编辑器(如Sublime文本)运行代码的raw_input,因为往往是由文本编辑器捕获。