我試圖從用戶輸入的密鑰中打印出字典中的值,但沒有出現。有任何想法嗎?爲什麼這不是在字典中打印出一個值?
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))
什麼是'title()'? – MxyL
@Keikoku - 使字符串像書名一樣的字符串方法。要親自看看,請在解釋器中運行:''happy birthday'.title()'。或者,這裏是[docs](http://docs.python.org/2.7/library/stdtypes.html#str.title)。 – iCodez
@iCodez哦,這很酷..我想。不看起來像一個非常聰明的轉換器,但我想它的工作...... – MxyL