2016-12-03 9 views
0

나는 유령이 나오는 집을 통해 모험 게임 인 파이썬 프로그램을 만들고 있습니다. 그 중 큰 것 중 하나는 owned_items이라는 목록입니다. 이것은 집에서 그들을 찾거나 처음에 그들에게 random.choice으로 주어질 때 그것에 덧붙여지는 "성냥 상자"또는 "토치"와 같은 문자열로 표현되는 항목을 얻습니다. 때로는 상황을 제시 할 때 특정 항목이 있는지 여부에 따라 선택 항목이 달라집니다.Python : if 문을 실행하지 않고 출력하기

내 코드 :

#bullets is the variable for pistol ammo. During my testing of this function, it is at 5 when it should start 
bullets=5 

owned_items=[] 
ronald_items=["a glass bottle", "a pistol", "a torch", "a small glass of  oil", "a box of matches", "a can of spray paint", "a small knife", "a pair of  surgical gloves", "a blessed amulet"] 
owned_items.append(random.choice(ronald_items)) 
ronald_items.remove(owned_items[0] 
owned_items.append(random.choice(ronald_items)) 
ronald_items.remove(owned_items[1]) 


#This part is in the actual definition where the problem appears when it should run 
def skeleton_choice(): 
    if "a glass bottle" in owned_items: 
     print('type "glass bottle" to attack the skeletons with that bottle') 
    if "a pistol" in owned_items and bullets>1: 
     print('type "shoot" to try firing your pistol at the skeletons') 
    if "a small knife" in owned_items: 
     print('type "small knife" to use your small knife against the skeletons') 
    if "a kitchen knife" in owned_items: 
     print('Type "kitchen knife" to use your kitchen knife against the skeletons') 
    if "a blessed amulet" in owned_items: 
     print('Type "amulet" to use the blessed amulet to make this a fairer fight') 
    print('Type "hands" to just fight the skeletons with your body') 
    print('Type "run" to try and get away from the skeletons') 

심지어 내가 문, 지문의 어느 것도 표시하지 않는 경우 내가이있는 항목의 3을 가지고 있음을 알고있을 때. 엘프와 다른 것보다는 ifs를 사용하고 있습니다. 왜냐하면 내가 가지고있는 모든 것에 대한 인쇄물을 보여주기를 원하기 때문입니다. 예를 들어 유리 병과 부엌 칼이있는 경우 병과 칼에 대한 인쇄 문구를 제공해야합니다.

+0

완전한 코드이고'def skeleton_choice()'에서 들여 쓰기 오류를 무시하면 함수가 실행되지 않도록 함수를 호출하지 않습니다. – roganjosh

+1

함수가 잘못된 의도를 가지고 있고'def skeleton_choice (input) '처럼 입력이 없다고 생각합니다. – Bryan

+0

질문과 관련이 있는지 또는 복사 및 붙여 넣기에 실패했는지 모르겠지만 if는 스켈레톤의 sk와 일치 해, def가 아니다. 내가보고있는 들여 쓰기에는 아무런 문제가 없습니다. –

답변

1

어디에서나 함수를 호출하지 않았으므로 그것이 작동하지 않습니다. 그냥 추가 :

skeleton_choice() 

끝 부분에 줄을 추가하십시오. 또한 줄에

ronald_items.remove(owned_items[0] 

괄호가 없습니다.

+0

누락 된 괄호는 실제 프로그램이 아니라 질문에만있는 것입니다. (들여 쓰기 문제를 해결하기 위해 복사 + 붙여 넣기를 신뢰하지 않고있었습니다.) 나는 바보 같은 인간입니다. 나는 그것이 해골을 어떻게 만드는지 수정하고 있었다. 함수 정의를 결정할 때 실제로 함수를 실행하는 것을 잊었습니다. 이제 완벽하게 작동합니다. –

0

코드가 작동합니다. 즉, skeleton_choice()에 호출을 추가 한 후입니다. 그냥 부르면 안 될까요?