Hero’s Inventory 程序简介
Hero‘s Inventory维护的是一个英雄的仓库。在这个程序中仓库是用一个元组表示的。元组中的字符串分别表示的是英雄所拥有的一个物品.
#演示元组的创建
#先创建一个空元组
inventory=()
#把元组当成条件来用
if not inventory:
print("You are empty-handed.")
input('\n\npress the enter key to exit.')
#创建一个带有一些内容的元组
invertory=('sword','armor','shield','healing protion')
#打印元组
print('\n The tuple inventory is:')
print('inventory')
#打印元组中的各个元素
print('\n Your items:')
for item in inventory:
print(item)
print('\n\npress the enter key to exit.')
Python 中任何值都可以当作条件使用。所以在上面的程序中,元组被当成了条件使用
if not inventory:
print("You are empty-handed.")
创建带有元素的元组。用下面这条语句给inventory赋值一个新元组
invertory=('sword',
'armor',
'shield',
'healing protion')
元组中各个元素可以用逗号隔开,在编写元组时也可以像这样跨越多行,只要是在逗号后面换行的就行。