2014-11-19 1 views
0

snowboots = False 인 동안 게임을 시작할 때 사전을 삭제하고 싶습니다. snowboots = True 일 때, 나는 그 방에 도달 할 수 있기를 원하며, 나는 snowboots를 집어 들고 그것을 참되게 만들고 싶다.항목 조건에 따라 사전 항목을 제거하는 방법

그것이 의미가있는 경우.

roomDirections = { 
    "hallEnt":{"e":"hallMid"}, 
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"}, 
    "snowRoom":{"n":"hallMid"}, 
    "giantNature":{"s":"strangeWall", "e":"riverBank", "w":"hallMid"}, 
    "strangeWall":{"s":"hallOuter", "e":"riverBank", "n":"giantNature"}, 
    "riverBank":{"e":"lilyOne", "w":"giantNature"}, 
    "lilyOne":{"e":"lilyTwo", "w":"riverBank", "n":"riverBank", "s":"riverBank"}, 
    "lilyTwo":{"e":"riverBank", "w":"lilyThree", "n":"riverBank", "s":"riverBank"}, 
    "lilyThree":{"e":"riverBank", "w":"lilyFour", "n":"riverBank", "s":"riverBank"}, 
    "lilyFour":{"e":"riverBank", "w":"treasureRoom", "n":"riverBank", "s":"riverBank"}, 
    "treasureRoom":{"w":"hallEnt"}, 
} 


roomItems = { 
    "hallEnt":["snowboots"], 
    "snowRoom":["lamp"], 
    "treasureRoom":["treasure"], 
    } 

snowboots = lamp = treasure = False 

이러한

내 사전 내 주장 변수입니다.
if "snowboots" == False: 
      del roomDirections["hallMid"] 
     else: 
      print ("you cannot go that way") 

은 그것에서 이동이 불가능하다 ... 그래서 때까지, roomDirections에서 hallMid을 제거하기 위해 의미되었다
elif playerInput in roomItems[currentRoom]: 
     print("picked up", playerInput) 
     invItems.append(playerInput) 
     playerInput == True 
     for i in range(0, len(roomItems[currentRoom])): 
      if playerInput == roomItems[currentRoom][i]: 
       del roomItems[currentRoom][i] 
       break 

snowboots이 덩어리가해야 할 생각했지만 그것은 아무튼 무엇 인 = TRUE 내가 일하는 것 같아, 내가 닫을 지, 아니면 완전히 벗어난거야?

편집 - 내 주요 게임 루프 -

while True: 
    playerInput = input("What do you want to do? ") 
    playerInput = playerInput.lower() 
    if playerInput == "quit": 
     break 

    elif playerInput == "look": 
     print(roomDescriptions[currentRoom]) 




    elif playerInput in dirs: 
     playerInput = playerInput[0] 
     if playerInput in roomDirections[currentRoom]: 


      currentRoom = roomDirections[currentRoom][playerInput] 
      print(roomEntrance [currentRoom]) 
     else: 
      print("You can't go that way") 

    elif playerInput == "lookdown": 
     if currentRoom in roomItems.keys(): 
      print ("You see", roomItems[currentRoom]) 
     else: 
      print ("You see nothing on the ground") 

    elif playerInput == "inventory" or playerInput == "inv": 
     print (invItems) 




    elif playerInput in roomItems[currentRoom]: 
     print("picked up", playerInput) 
     invItems.append(playerInput)  
     for i in range(0, len(roomItems[currentRoom])): 
      if playerInput == roomItems[currentRoom][i]: 
       del roomItems[currentRoom][i] 
       break 

    elif playerInput in invItems: 
     print("dropped", playerInput) 
     roomItems[currentRoom].append (playerInput) 
     for i in range (0, len(invItems)): 
      if playerInput == invItems[i]: 
       del invItems[i] 
       break 
    else: 
     print ("I don't understand") 
+0

'''elif'' 스위트가'''snowboots''에''True''를 할당하기를 원한다는 말입니까? 그렇다면 단지 할당 문을 추가하십시오. '''playerInput == True'''는 효과가없는 문장입니까? – wwii

+0

나는 할당 문이 미안하다는 것을 모른다. 나는 파이썬에 대해 아주 새로운 것이다. 편집 : 'elif'스위트는 게임의 세 항목 모두를 선택하기도하지만, 플레이어가 오른쪽 방에있을 때만 가능합니다. – kebab

+0

'''a = 3'''은 할당 문입니다. 'a'. – wwii

답변

1

내가 알고 있듯이 특정 조건을 추가하여 특정 이탈을 허용 할지를 결정해야합니다. 현재 각 방의 지시 사항을 매핑하는 사전, 플레이어가 각 항목을 갖고 있는지 여부를 유지하기위한 변수 및 각 방의 항목 목록과 플레이어 인벤토리가 있습니다.항목 변수는 중복됩니다. 단순히 재고를 확인할 수 있습니다.

제안 된 방법은 필요한 항목을 얻거나 잃어 버렸을 때 퇴실을 추가 및 제거하는 것입니다. 이 작업을 수행 할 수 있지만 제거 할 exit를 찾는 복잡성은 처음부터 무시해야하는 모든 작업입니다. 제거 된 경우 복원하는 것이 필요에 따라 필터링하는 것보다 어렵습니다. 플레이어가에 필요한 항목을 제거 할 수 있도록

elif playerInput in dirs: 
    playerInput = playerInput[0] 
    if playerInput in roomDirections[currentRoom]: 
     newRoom = roomDirections[currentRoom][playerInput] 
     required = requirements.get(newRoom) 
     if required and required not in invItems: 
      print("You can't go that way. " + reasons[required]) 
     else: 
      currentRoom = newRoom 
      print(roomEntrance [currentRoom]) 
    else: 
     print("You can't go that way") 

또한 그것을 만들 수 있습니다 :

requirements = {'snowRoom': 'snowboots', 'darkCave': 'lamp'} 
reasons = {'snowboots': "You'd sink into the snow.", 
      'lamp': "It would be too dark to see."} 

그런 다음 조건이 만족 아니었다면 방향을 무시하려면 다음을 사용할 수 있습니다 여기에 하나의 접근 방식 방 :

elif playerInput in invItems: 
    if playerInput != requirements[currentRoom]: 
     print("dropped", playerInput) 
     roomItems[currentRoom].append (playerInput) 
     invItems.remove(playerInput) 
    else: 
     print("You still need " + playerInput + ". " + reasons[required]) 

그것은 방 실제로 항목, 다른 객실에 대한 링크 및 요구 사항의 목록을 포함하는 더 객체 지향 접근 방식을 가지고하는 것이 할 수 있습니다. 당신은 또한 항목에 동의어를 추가하는 것과 같은 일을 할 수 있습니다.

0

내가 여기 당신의 실수를 지적하자. snowboots (개체)이지만 "snowboots" (문자열)을 비교하고 있습니다. 따라서 "snowboots"if 상태로 비교하면 false를 반환하고 결과적으로 항상 else 부분으로 이동합니다. 다음 코드를 사용하여 도움이되는지 알려주십시오.

snowboots = False 
if snowboots == False:    ## notice the absence of double quotes here 
    del roomDirections["hallMid"] 
else: 
    print ("you cannot go that way") 

편집 : 더 명확하게하기 위해서 는 그 del 작품을 표시하도록 코드를 수정했습니다. 나는 제거한

roomDirections = { 
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"}, 
} 

print roomDirections ## print dictionary before your deleting. 

snowboots = lamp = treasure = False 

if snowboots == False:    ## notice the absence of double quotes here 
    del roomDirections["hallMid"] 
else: 
    print ("you cannot go that way") 

print roomDirections    ## print dictionary after deleting 

출력 :

{'hallMid': {'s': 'snowRoom', 'e': 'giantNature', 'w': 'hallEnt'}} ## before deleting 

    {}  ## after deleting. 
+0

여전히 방 (hallMid)을지나 다음 방으로 옮겨 가게합니다. – kebab

+0

@kebab "방을 통해 움직 이도록 내버려 둬"나는 이해하지 못합니다. –

+0

나는 그것이 작동한다는 것을 알고있다. 내 프로젝트의 다른 부분에서 그것을 사용하고 있지만, 한 번 복도는 방에서 제거된다. 나는 복도에서 새로운 방으로 이동할 수 없어야한다. 그래도 나는 할 수 있니? – kebab

0

playerInput은 문자열입니다. 문자열을 부울 값에 매핑해야하는 것처럼 보입니다. 당신은 개념적인 문제 w를 가지고있는 것처럼

items = {'snowboots' : False, 
     'lamp' : False, 
     'treasure' : False} 

다음 변경

if "snowboots" == Falseif not items['snowboots']

playerInput == True-items[playerInput] = True

snowboots = lamp = treasure = False

for item in items: 
    items[item] = False 

이 보이는 i 문자열과 이름을 조합하여 코드에서 혼합하는 것처럼 보입니다. "foo"foo과 다릅니다.

>>> 
>>> foo = 2 
>>> "foo" = 2 
SyntaxError: can't assign to literal 
>>> foo = "foo" 
>>> foo 
'foo' 
>>> "foo" = foo 
SyntaxError: can't assign to literal 
>>> foo = False 
>>> bool("foo") 
True 
>>> bool(foo) 
False 
>>> 

모든 코드를 검토하고 유사한 코드를 모두 해결해야합니다.