【发布时间】:2020-09-18 09:58:28
【问题描述】:
我想显示一副牌并输出牌的数量。
以下是卡片: 旁注:中间一列是力量,最后一列是该名称的牌数。
Admiral,30,1
General,25,1
Colonel,20,2
Major,15,2
Captain,10,2
Lieutenant,7,2
Sergeant,5,4
Corporal,3,6
Private,1,10
数字列表示该名称的卡片数量。我希望它使用 append 打印出以下内容。我知道我想使用 for 循环将排名列附加到卡片组,但我不确定如何编写代码。
输出应该是:
['Admiral', 'General', 'Colonel', 'Colonel', 'Major', 'Major', 'Captain', 'Captain', 'Lieutenant', 'Lieutenant', 'Sergeant', 'Sergeant', 'Sergeant', 'Sergeant', 'Corporal', 'Corporal', 'Corporal', 'Corporal', 'Corporal', 'Corporal', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private']
There are 30 cards in the deck.
我的代码在这里:
while True:
text = rankFile.readline()
#rstrip removes the newline character read at the end of the line
text = text.rstrip("\n")
if text=="":
break
data = text.split(",")
rankList.append(data[0])
powerList.append(int(data[1]))
numberList.append(int(data[2]))
for i in range(0, len(rankList)):
rankList.append(numberList[i]) # this wont work since number is an integer but how can I modifiy this...
rankFile.close()
print(50*"=")
print("\t\tLevel 3 Deck")
print(50*"=")
print (rankList)
print (powerList)
print (numberList)
【问题讨论】:
标签: python list loops for-loop append