2016-12-26 2 views
0

많은 사람들처럼 파이썬도 처음이에요. 사용자에게 ID를 요청한 스 니펫을 작성한 다음 ID의 길이가 정확히 6 자리인지 확인합니다. 그런 다음 코드는 사용자에게 ID를 확인하도록 요청하고 잘못 입력했는지 다시 확인하도록합니다. 사용자가 입력 내용이 올바른지 확인하면 위치 ID를 묻고 같은 경로를 따릅니다. 두 ID가 모두 확인되면 사용자는 나머지 프로젝트로 이동할 수 있습니다.파이썬 - 중첩 된 IF

이것은 모든 사용을 시작할 때 입력해야하는 항목입니다.

문제는 3면으로 실행 중입니다.

1) 유효한 항목이 다른 사람 동안 늘 (그러나 상관없이 101,256 작품의 나는 EMPID 101290 입력 할 수 있습니다 때로는 나를 말한다 -에 모두 6 자리입니다)

2) 입력 "1" 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 

여기 루프가 존재합니다. 즉, 두 번째 경우에만 루프가 계속 실행됩니다. 변수가 참일 경우

+0

통찰력과 정정에 감사드립니다. 그것은'def' 블록들 사이에'next'를 놓지 않고'elif'를 제안 된'else'로 변경할 때 들여 쓰기를 기대하지 않으면 멈추고 문법 오류를줍니다. –

+0

'next()'는 반복자에 사용되는 함수입니다. 코드에서 아무데도 필요하지 않습니다. 또한'else'는'while'이 아니라'if'와 함께 사용됩니다. – dhishan

+0

위에 편집 된 코드를 게시하십시오. 내가 그것을 봐 보자. – dhishan