2016-04-08 10 views
0

私はここに自分のコードに妥当性チェックを追加しようとしていますので、値("A"), ("B") or ("C")しか許されません。 len()部分が削除された場合、3文字のいずれかの文字列は使用できますが、これらの文字のいずれかが使用されていない場合は、期待どおりに動作します。 len()を追加すると、効果がないように見え、len()が正しい値を出力してもそれをバイパスします。if ... not inとlen()チェックが適用されていません

どうすれば解決できますか?

ありがとうございます!あなたは("a", "b", "c")はタプルに割り当てるスピード、使用中の(この場合は無視できる)ゲインはリストよりも高速であるかどうコメント(秒)毎:

classCheck = False 
studentclass=input("What class are you in?\n A\n B\n C\n ") 
print (len(studentclass)) 
while classCheck != True: 
    if ("a" or "b" or "c") not in studentclass.lower() and len(studentclass) != 1: 
     print ("You must enter a valid class") 
     studentclass=input("What class are you in?\n A\n B\n C\n ") 
    else: 
     classCheck = True 
+8

'( "A" 又は "B" 又は "C")についても同様である

"a" not in studentclass.lower() 

が'の ' ""'単に等しいです。 – RemcoGerlich

答えて

1

私はあなたがif studentclass.lower() not in ["a", "b", "c"]

Editを使用するためのものだと思います。

+0

これは私の問題を解決しました。ありがとうございます:) – OGBites

+1

注意してください(マイクロ最適化を確認してください)、 'not'( 'a'、 'b'、 'c') 'は*わずかに高速です。 –

1

あなたはそれを行うことができます:Pythonは英語を話すのではなくので

classCheck = False 
studentclass=input("What class are you in?\n A\n B\n C\n ") 
print (len(studentclass)) 
while classCheck != True: 
    if studentclass.lower() not in ['a', 'b', 'c']: 
     print ("You must enter a valid class") 
     studentclass=input("What class are you in?\n A\n B\n C\n ") 
    else: 
     classCheck = True 

あなたのソリューションは、動作しませんでしたPythonのように、あなたは何時に:

("a" or "b" or "c") not in studentclass.lower() 

これは、最初の時間でのeval:

("a" or "b" or "c") 

bool(expr)== Trueのような最初の式を返すので、ここでは "a"となります。 Pythonは評価: 'B' または 'C'

関連する問題