2014-09-13 8 views
0

출력 결과가 예와 아니오 모두에 대한 인쇄 대답을 생성합니다. 나는 을 생성하고 싶습니다. 사용자가 입력 한대로 대답은 "예"이고 답변은 "아니오"입니다.파이썬 게임이 내가 원하는 것을 출력하지 않습니다.

import time 
import random 
time.sleep(0) 
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") 
print("Welcome to the zombie apocalypse simulator!") 
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") 
print() 
time.sleep(0) 


print("You wake up in the backseat of an old 1995 suburu, shirtless and confused.") 
time.sleep(0) 
print("Looking out the dusty window, you can make out the skyline of a distant") 
print("forgotten metropolis.") 
print() 
time.sleep(0) 

def ask(question): 
    answer = input(question + " {y/n}") 
    return answer in ['Yes','YES','y', 'Y','yes', 'n', 'N', 'no', 'NO', 'No'] 

while ask("You see a backpack in the from seat of the car. Do you take it?"): 
    if ['Yes','YES','y', 'Y','yes']: 
     print("You look inside only to find a brown hoodie, a utility knife, a can of tuna") 
     print("and a half full water bottle.") 
    if ['n', 'N', 'no', 'NO', 'No']: 
     print("You leave the bag behind.") 
    break 
+0

힌트 : 목록은 '[]'빈 목록이 아닌 한 항상 참입니다. 그러므로''예 ','예 ','y ','Y ','예 ']'가 항상 참일 경우. 'ask()'함수를 고쳐야한다. 왜냐하면'in'은 생각하는 것처럼 작동하지 않기 때문이다. 추신. 코드의 들여 쓰기를 수정하십시오. –

+0

들여 쓰기가 실제로 파이썬에서 중요한 점에 유의하십시오. 실행될 수있는 형식으로 코드를 제시하십시오. –

답변

1

answer = 'YES' 경우 return answer in ['Yes', .. , 'No'] 반환 True 그렇지 않으면 False. 기본적으로 in은 목록에 주어진 요소가 포함되어 있는지 여부를 알려줍니다. 그래서, 당신의 ask 기능에서는, 사용자는 루프가 실제로 있다면 확인하는 코드 내부 곳이 없습니다

# ask question, if answer is positive 
while True: 
    # check if yes, do something 
    # else do something. 
    break 

처럼 행동하면서 True 값이 반환되는 가능한 대답 중 하나가 다음을하게 입력하면 대답은 예 또는 아니오입니다. 따라서 출력하려면 해당 값을 확인하십시오. (질문의 코멘트에있는 힌트를보십시오)

관련 문제