我试图从用户输入的密钥中打印出字典中的值,但没有出现。有任何想法吗?为什么这不是在字典中打印出一个值?
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