저는 최근에 아주 기본적인 문제 해결 시스템의 코드를 완성했습니다. 난 그냥 몇 가지 유효성 검사를 추가하려고 그래서 만약 사용자가 질문에 예 또는 아니오를 입력하지 않으면 쉘에 '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")
첫째, 예를 얻는 함수를 작성 또는 전혀 사용자로부터. 당신이 원하는대로 일하는 것에 초점을 맞추십시오. 그리고 나서 모든 다른 질문에 사용할 수 있습니다. – khelwood
'else'를 사용하십시오! 하지만 알 레디가 알 것 같아. –
아직도 어려움을 겪고 있지만, 몇 가지 질문을하거나 전혀 대답하지 않습니다. 제발 도와 줄 수있어? @khelwood – Zac