2013-11-28 6 views
0

나는 사용자가 입력 한 키로 사전에 값을 출력하려고하지만 아무 것도 나오지 않으려 고합니다. 어떤 아이디어?왜 사전에 값을 출력하지 않습니까?

u_items=input("Enter 'list' to see a list of items, or 'Book', 'Apple', 'Toy', 'Pumpkin', 'Bowl' or 'Purse' to see the item's price: ") 
u_items=u_items.title() 

inventory={'Book':'18', 'Apple':'3', 'Toy':'7', 'Pumpkin':'9', 'Bowl':'5', 'Purse':'30'} 

if u_items in inventory[u_items] == True: 
    print("A " + u_items + "costs $" + inventory[u_items]) 

elif u_items == 'List': 
    print(items(inventory)) 
+0

'title()'은 무엇입니까? – MxyL

+0

@Keikoku - 문자열을 책 제목처럼 만드는 문자열 메서드입니다. 직접 보시려면 인터프리터에서''happy birthday '.title()'을 실행하십시오. 또는 다음은 [docs] (http://docs.python.org/2.7/library/stdtypes.html#str.title)입니다. – iCodez

+0

@iCodez 오, 꽤 멋지다. 나는 생각한다. 매우 똑똑한 변환기처럼 보이지는 않지만 그 일을하는 것 같아요. – MxyL

답변

1

if 문이 잘못 구성되었습니다. 그것은 다음과 같이해야합니다 :

if u_items in inventory: 

이 테스트 u_items (사용자가 입력 한 키가) 사전에있는 경우.

u_itemsinventory의 색인으로 반환 된 값인 경우 u_items으로 표시되었습니다. 따라서 사용자가 'apple'을 입력하면 코드는 다음과 같이 해석됩니다.

if 'Apple' in '3' == True: 
+0

v 많은 감사드립니다! – rs19

+0

및 [인벤토리] 항목이 작동하지 않습니다. 내 구문을 잘못 추측 했나요? – rs19

+0

@ rs19 - 아, 네. 나는 그것을 놓쳤다. 'item'은'dict'의 메소드이므로, 이렇게해야합니다 :'inventory.items()' – iCodez

관련 문제