2014-10-03 62 views
-1

我是一個完整的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

回答

1

如果本書使用Python 2.x,則只有raw_input可以工作。如果你使用的是Python 3.x,使用input()將會很好。

1

在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,因爲往往是由文本編輯器捕獲。