2016-12-26 8 views
0

多くのここと同じく、私はPythonが初めてです。私は、ユーザーにIDを伝えるようスニペットに取り組んでおり、IDが正確に6桁であるかどうかを確認します。その後、コードはユーザーにIDを確認するように要求し、誤って入力した場合はリセットします。ユーザが自分のエントリが正しいことを確認すると、ロケーションIDを要求し、同じパスに従います。両方のIDが確認されたら、ユーザーはプロジェクトの残りの部分に移動できます。Python - 入れ子にされたIF

これは、使用するたびに入力する必要があるものです。

私は3面で走っています。

1)私はEMPID 101290を入力することができますし、時にはそれは文句を言わない他の人にそれを(関係なく101256点の作品ながら、それは有効なエントリです私に語った - に「1」を両方とも6桁の数字です)

2)を入力しますIDを確認すると、コードはブロック2に移動して位置IDを要求しますが、ユーザーが従業員IDが間違っていると言うために「2」を入力すると、とにかく移動します。

ここで変更が必要なものについてアドバイスをいただけますか?

import time 
print('What is your employee ID?') #user assigned ID 
empID = input() 
while empID != 0: 
    print('Try again.') 
    empID = input() 

# employee ID is only 6 digits in length, no letters 
    if len(empID) != 6: 
     print('Try again.') 
    elif len(empID) == 6: 
     print('Thank you. Your ID is set to ' + empID + '.') 
     time.sleep(.5) 
     print('Is this correct?''' 
       '[1] Yes [2] No ') 
     yesNo = input() 
     while True: 
      yesNo == '1' 
      print('Thank you. ID set.') 
      break 
# reset ID 
     else: 
      print('ID has been reset. Please enter your employee ID.') 
      empID = input() 
      break 
    break 

#Store Location ID 
print('What is your Location ID?') 
locID = input() 
while locID != 0: 
    print('Try again.') 
    locID = input() 

# store locations are 3-5 digits 
# TODO: prepend any input with less than len 5 with 0 
    if len(locID) != 5: 
     print('Try again.') 
    elif len(locID) == 5: 
     print('Thank you. Your location is set to ' + locID + '.') 
     time.sleep(.5) 
     print('Is this correct?''' 
       '[1] Yes [2] No ') 
     yesNo = input() 
     while True: 
      yesNo == '1' 
      print('Thank you. Location ' + locID + 'set.') 
      break 
     else: 
      print('Location ID has been reset. Please enter your location code.') 
      empID = input() 
      break 
     break 
    break 
#next 
+0

'if'ループは存在しません。 –

+0

あなたのインデントが間違っています、ここの質問のコードは有効な構文ではありません。あなたがそれを修正しない限り、我々は助けることができません。質問を編集し、質問に再度コードをコピーし、すべてを選択して中括弧ボタンをクリックしてインデントします(コードブロックとしてフォーマットする)。 –

+0

さて、私はそれを何か悪いと呼んだ。 –

答えて

2

コードにはいくつかのバグがあります。私はどうなるのか

while True: 
    yesNo == '1' 

yesNo == '1'ユーザーの入力に応じて、trueまたはfalseを返す条件文ですが、それはどこかの条件として使用されていない

if len(empID) != 6: 
    print('Try again.') 
elif len(empID) == 6: 

`elif len(empID) == 6:` is redundant.. a simple else will do 

は次のとおりです。 に関数を定義します

def isEmpID(id): 
    ''' 
    Employee ID is 6 characters in Length 
    ''' 
    if len(id) != 6: 
     return False 
    return True 


def isStoreID(id): 
    ''' 
    Store ID is 3-6 characters in Length 
    Note: The function when called with id, checks if the length is between (exclusive) 3 and (inclusive) 6 and returns true if condition is satisfied else false which is the default return policy 
    ''' 
    if 3 < len(id) <= 6: 
     return True 
    return False 


validEmpID = False 
validStoreID = False 
while not (validEmpID and validStoreID): # Both has to be True to exit the loop, Otherwise the condition continues to go to True. 
    if not validEmpID: 
     print('Enter Employee ID:') 
     empID = input() 
     validEmpID = isEmpID(empID) 
     if not validEmpID: 
      print('Invalid Employee ID\nTry Again...\n') 
      continue 
    print('Enter Store ID:') 
    strID = input() 
    validStoreID = isStoreID(strID) 
    if not validStoreID: 
     print("Invalid Store ID\nTry Again!...\n") 
     continue 

ここでループが存在する、つまり、両方の場合にのみコードが実行され続けます変数はTrueです。

+0

私は洞察と訂正を感謝します。私が 'def'ブロックの間に' next'を落とし、 'elif'を提案された' else'に変更するときインデントを期待しない限り、文法エラーが発生し続けます。 –

+0

'next()'はイテレータに使用される関数です。あなたのコードのどこにでもあなたはそれを必要としません。また、 'else'は' while'ではなく 'if'でのみ使用されます。 – dhishan

+0

編集したコードを上記に投稿してください。私はそれを見てみましょう – dhishan

関連する問題