2017-05-02 1 views
-1

저는 최근에 아주 기본적인 문제 해결 시스템의 코드를 완성했습니다. 난 그냥 몇 가지 유효성 검사를 추가하려고 그래서 만약 사용자가 질문에 예 또는 아니오를 입력하지 않으면 쉘에 'Invalid input'이 인쇄되지만 정확하게 수행하는 방법을 모르는지 확인합니다. 누군가 나를 도울 수 있습니까? 매우 감사.어떻게 파이썬에서 오류를 출력합니까?

내 코드 :

prompt = "> " 

print ("screen question one here") 
screen = input(prompt) 
if screen == "yes": 
    print ("screen question two here") 
    screen2 = input(prompt) 
    if screen2 == "yes": 
     print ("screen question three here") 
     screen3 = input(prompt) 
     if screen3 == "yes": 
      print ("screen advice one here") 
     elif screen3 == "no": 
      print ("screen adivce two here") 
    elif screen2 == "no": 
     print ("camera question one here") 
     camera = input(prompt) 
     if camera == "yes": 
      print ("camera question two here") 
      camera2 = input(prompt) 
      if camera2 == "yes": 
       print ("camera advice one here") 
      elif camera2 == "no": 
       print ("camera advice two here") 
     elif camera == "no": 
      print ("water question one here") 
      water = input(prompt) 
      if water == "yes": 
       print ("water question two here") 
       water2 = input(prompt) 
       if water2 == "yes": 
        print ("water advice one here") 
       elif water2 == "no": 
        print ("water advice two here") 
      elif water == "no": 
       print ("buttons question one here") 
       buttons = input(prompt) 
       if buttons == "yes": 
        print ("buttons advice one here") 
       elif buttons == "no": 
        print ("buttons advice two here") 
elif screen == "no": 
    print ("battery question one here") 
    battery = input(prompt) 
    if battery == "yes": 
     print ("battery question two here") 
     battery2 = input(prompt) 
     if battery2 == "yes": 
      print ("battery advice one here") 
     elif battery2 == "no": 
      print ("battery advice two here") 
    elif battery == "no": 
     print ("wifi question one here") 
     wifi = input(prompt) 
     if wifi == "yes": 
      print ("wifi advice one here") 
     elif wifi == "no": 
      print ("wifi advice two here") 
+0

첫째, 예를 얻는 함수를 작성 또는 전혀 사용자로부터. 당신이 원하는대로 일하는 것에 초점을 맞추십시오. 그리고 나서 모든 다른 질문에 사용할 수 있습니다. – khelwood

+1

'else'를 사용하십시오! 하지만 알 레디가 알 것 같아. –

+0

아직도 어려움을 겪고 있지만, 몇 가지 질문을하거나 전혀 대답하지 않습니다. 제발 도와 줄 수있어? @khelwood – Zac

답변

1

여기 당신이 그것을 할 수있는 하나의 방법입니다.

사용자가 예 또는 아니오를 얻는 함수를 정의하십시오. 사람들이 원하는 경향이있는 것은 사용자가 적절한 응답을 줄 때까지 질문을 반복하는 것입니다. 즉이 함수가하는 일입니다. 당신은 당신이 잘못된 입력을받을 때 스크립트를 종료하려면

한편
def yesorno(question): 
    while True: 
     print(question) 
     answer = input('> ').strip().lower() 
     if answer in ('y', 'yes'): 
      return True 
     if answer in ('n', 'no'): 
      return False 
     print("Invalid input") 

, 당신이 할 수 있습니다 :

def yesorno(question): 
    print(question) 
    answer = input('> ').strip().lower() 
    if answer in ('y', 'yes'): 
     return True 
    if answer in ('n', 'no'): 
     return False 
    exit('Invalid input') 

exit은 단순히 전체 프로그램을 종료합니다. 반드시 추천할만한 것은 아닙니다.

어느 쪽이든,이 같은 yesorno를 사용할 수 있습니다

if yesorno("Question 1"): 
    # User answered yes 
    if yesorno("Question 2A"): 
     # User answered yes 
    else: 
     # User answered no 
else: 
    # User answered no 
    if yesorno("Question 2B"): 
     ... 
+0

이것은 완벽하게 작동합니다! 스트립()이이 코드에서하는 것은 무엇입니까? 고맙습니다. @khelwood – Zac

+0

['strip'] (https://docs.python.org/3/library/stdtypes.html#str.split)은 문자열에서 주변 공간을 제거하므로 사용자가''yes ''를 입력하면 ''예''로 변환 될 수 있습니다. – khelwood

관련 문제