2012-03-27 12 views
0

私はプログラミングとPythonの新機能です。私はスクリプトを書いています。顧客が空白を入力すると、スクリプトを終了します。 の質問はどうすれば正しいのですか? これは私の試みですが、私は例Pythonで空白の入力を確認する方法

userType = raw_input('Please enter the phrase to look: ') 
userType = userType.strip() 

line = inf.readline() 
while (userType == raw_input) 
    print "userType\n" 

    if (userType == "") 
     print "invalid entry, the program will terminate" 
     # some code to close the app 

答えて

2

についてあなたが提供されたプログラムが有効なPythonプログラムではありません

間違っていると思います。あなたは初心者であるため、プログラムに少し変更があります。これが実行され、私はそれが何をすべきか理解しています。構造が明確でないと、あなたがそれらを必要とするようなものを変更する必要があります。

これが唯一の出発点です。

userType = raw_input('Please enter the phrase to look: ') 
userType = userType.strip() 

#line = inf.readline() <-- never used?? 
while True: 
    userType = raw_input() 
    print("userType [%s]" % userType) 

    if userType.isspace(): 
     print "invalid entry, the program will terminate" 
     # some code to close the app 
     break 
0

空白を削除するためにストリップを適用した後、代わりにこれを使用する:

if not len(userType): 
    # do something with userType 
else: 
    # nothing was entered 
0

あなた可能性あなたの入力でstrip all whitespaces、何が残っているかどうかを確認します。

import string 

userType = raw_input('Please enter the phrase to look: ') 
if not userType.translate(string.maketrans('',''),string.whitespace).strip(): 
     # proceed with your program 
     # Your userType is unchanged. 
else: 
     # just whitespace, you could exit. 
3

私はこれが古いことを知っていますが、これは将来誰かを助けるかもしれません。私は正規表現でこれを行う方法を考え出した。ここに私のコードは次のとおりです。

import re 

command = raw_input("Enter command :") 

if re.search(r'[\s]', command): 
    print "No spaces please." 
else: 
    print "Do your thing!" 
関連する問題