1

Here is the file:

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

I need to display the deck (in list form) and output how many cards there are. The last number in the file represents how many cards there are. So there is 1 Admiral card in the deck, 2 Colonel, 10 Private, etc.

Below is my code so far:

numFile = open("ranks.dat", "r")

#list fields
rankList = []
skillList = []
numberList = []

while True:
    text = numFile.readline()
    text = text.rstrip("\n")
    if text=="":
        break
    info = text.split(",")
    rankList.append(info[0])
    skillList.append(int(info[1]))
    numberList.append(int(info[2]))


numFile.close

print("Rank\t\tSkill\t\tNumber")
print(45*"=")
for i in range(len(rankList)):
    print("%-10s\t%3i\t\t%3i" %(rankList[i], skillList[i], numberList[i]))

How do I add the deck of cards list?

How do I add the deck of cards in a list? This is how the list is supposed to look:

========================================
        Level 3 Build Deck
========================================
['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']


unknownmac
  • 41
  • 3
  • And what's the *problem* with that code? – jonrsharpe Jun 04 '20 at 19:33
  • @jonrsharpe there is no problem with that code, I need to add the deck of cards lists from the file, but I am not sure how to do that – unknownmac Jun 04 '20 at 19:35
  • 1
    So could you rephrase your question -- or open a new one -- to do with reading from a file in Python. Ideally, give us an example of the file you want to read. –  Jun 04 '20 at 19:57
  • @mrblewog yea sure I can do that, how do I give you an example of the file you want to read -- is there any way to attach it? I put the content of the file at the top of the file, is that enough? – unknownmac Jun 04 '20 at 20:04
  • It seems your code works okay in displaying the deck of cards from your file example. Are you only missing showing the total number of cards? The number of cards is just `sum([int(x) for x in numberList])` – DarrylG Jun 04 '20 at 20:08
  • Yeah, that's fine. So with the content `Admiral,30,1` etc in a file called `ranks.dat` and a python file called `main.py` with your Python code, I can read the the file and print out some rows... is that what you expect? –  Jun 04 '20 at 20:08
  • @mrblewog I just added how I want the output to look, is that helpful? – unknownmac Jun 04 '20 at 20:20
  • Very much, yes. You already know about the range(n) function and about appending to lists, so you have the building blocks.... –  Jun 04 '20 at 20:31
  • @mrblewog so what should my next step be? – unknownmac Jun 04 '20 at 20:46

1 Answers1

0

Refactoring Posted Software

The following simplifies posted code and creates same output

Note: rename file ranks.txt (i.e. txt file normally end with txt suffix).

with open("ranks.txt", "r") as f:
  rankList, skillList, numberList = zip(*[x.rstrip().split(',') for x in f])

skillList = [int(x) for x in skillList]
numberList = [int(x) for x in numberList]

print("Rank\t\tSkill\t\tNumber")
print(45*"=")
for i in range(len(rankList)):
    print("%-10s\t%3i\t\t%3i" %(rankList[i], skillList[i], numberList[i]))

To Build Deck List

Inspired by

Build Deck

deck = [rank for rank, number in zip(rankList, numberList) for i in range(number)]
print(deck)

Or equivalently using for loops

deck = []
for i in range(len(rankList)):
  rank = rankList[i]
  number = numberList[i]
  for repeat in range(number):
    deck.append(rank)

Output

['Admiral', 'General', 'Colonel', 'Colonel', 'Major', 'Major', 'Captain', 'Captain', 'Lieutenant', 'Lieutenan
t', 'Sergeant', 'Sergeant', 'Sergeant', 'Sergeant', 'Corporal', 'Corporal', 'Corporal', 'Corporal', 'Corporal
', 'Corporal', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Priva
te', 'Private']

Explanation

Build Deck List Comprehension

deck = [rank for rank, number in zip(rankList, numberList) for repeat in range(number)]

Create sequence of pairs of rank and number of that rank

zip(rankList, numberList)

Iterate over these pairs

 for rank, number zip(rankList, numberList)

Repeat rank based upon the number in each pair

 [rank for rank, number in zip(rankList, numberList) for repeat in range(number)]

Complete Software

numFile = open("ranks.dat", "r")

#list fields
rankList = []
skillList = []
numberList = []

while True:
    text = numFile.readline()
    text = text.rstrip("\n")
    if text=="":
        break
    info = text.split(",")
    rankList.append(info[0])
    skillList.append(int(info[1]))
    numberList.append(int(info[2]))

numFile.close

print("Rank\t\tSkill\t\tNumber")
print(45*"=")
for i in range(len(rankList)):
    print("%-10s\t%3i\t\t%3i" %(rankList[i], skillList[i], numberList[i]))

deck = []
for i in range(len(rankList)):
  rank = rankList[i]
  number = numberList[i]
  for repeat in range(number):
    deck.append(rank)

print(deck)
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • @DarryIG is there anyway not to use the iterate? and zip? I have never learned that – unknownmac Jun 04 '20 at 21:32
  • @unknownmac--sure--I'll expand to do it with a normal for loop. – DarrylG Jun 04 '20 at 21:34
  • @unknownmac--added version using for loops. Does it make sense? – DarrylG Jun 04 '20 at 21:38
  • @DarryIG Thanks a lot! Now when I run it, its not showing anything tho. Perhaps I am making a mistake? – unknownmac Jun 04 '20 at 21:42
  • @DarryIG numFile = open("ranks.dat", "r") #list fields rankList = [] skillList = [] numberList = [] deck = [] while True: text = numFile.readline() text = text.rstrip("\n") if text=="": break info = text.split(",") for i in range(len(rankList)): rank = rankList[i] number = numberList[i] for repeat in range(number): deck.append(rank) numFile.close – unknownmac Jun 04 '20 at 21:44
  • @unknownmac--I added the complete software at the end which is just an addition to your original software. – DarrylG Jun 04 '20 at 21:48
  • @DarryIG Thanks a lot! – unknownmac Jun 04 '20 at 21:49