Question: Im new to programming and am trying to write a program in Python. I cannot get the character list to populate into the correct format.
Im new to programming and am trying to write a program in Python.
I cannot get the character list to populate into the correct format.
Any help would be appreciated, I have posted my code below.
These are the instructions:
Stage 2
Write the code for functions read_file() and display_characters() as specified above. Add code to call these two functions to ensure they are working correctly. You may write your own read_file() function or you may use the one provided for you (included in the provided file).
the code should output:
but my output is:
This is my code:
##Must modify with your solution. Place your solution and comments in this file.
import random
# Function read_file() - place your own comments here... : )
def read_file(filename):
character_list = [] infile = open(filename, \"r\")
index = 0 line = infile.readline()
# While not end of file reached i.e. empty string not returned from readline method.
while line != '': name = line.strip(' ') line = infile.readline() secret_id = line.strip(' ') line = infile.readline()
# Split line into no battles, no won, no lost, etc. info_list = line.split() is_hero = info_list[0] no_battles = int(info_list[1]) no_won = int(info_list[2]) no_lost = int(info_list[3]) no_drawn = int(info_list[4]) health = int(info_list[5])
# Create new list to store individual character information new_character = [name, secret_id, is_hero, no_battles, no_won, no_lost, no_drawn, health] # Add new character to character_list (creating a list of lists) character_list.append(new_character) # Read next line of file. line = infile.readline()
return character_list
# Function display_characters() - place your own comments here... : )
def display_characters(character_list, display_type): character_list = [] display_type = []
# Determine if to display all characters, only heroes or only villains. # only hero characters to be displayed if display_type == 1: for i in range(len(character_list)): if character_list[i].get_is_hero(): print(\"- \", end=\"\") print(character_list[i], end=\"\") print(\" -\") print(\"---------------------------------------------------\")
# only villain characters to be displayed
elif display_type == 2: for i in range(len(character_list)): if not character_list[i].get_is_hero(): print(\"- \", end=\"\") print(character_list[i], end=\"\") print(\" -\") print(\"---------------------------------------------------\")
# all characters to be displayed
else: for i in range(len(character_list)): print(\"- \", end=\"\") print(character_list[i], end=\"\") print(\" -\") print(\"---------------------------------------------------\") print(\"===================================================\")
#This line will eventually be removed - used for development purposes only.
print(\"In function display_characters()\")### Define list to store character information
character_list = []
display_type = \" \"
#Place your code here
# # Using a for loop to iterate over the list of lists (characters)
# for character in character_list:
# print(character)
# ... or ... We can use a while loop to achieve the same thing :)
index = 0
while index
print(character_list[index])
index = index + 1
for i in range(0, 51):
print(\"=\", end=\"\")
print(\"\")
for i in range(0, 1):
print(\"-\" + format(\"Character (heroes and villains) Summary\", '^49s') + \"-\")
for i in range(0, 51): print(\"=\", end=\"\") print(\"\")
for i in range(0, 1): print(\"-\" + format(\"P W L D Health \", '>49s') + \"-\")
for i in range(0, 51): print(\"-\", end=\"\") print(\"\")
for i in range(0, 1): for i in len(character_list): print(\"-\" + format(character_list[0], '>49s') + \"-\")
for i in range(0, 51): print(\"-\", end=\"\") print(\"\")
for i in range(0, 1): print(\"=\", end=\"\") print(\"\")
# only hero characters to be displayed
### Place your code here... : )
print(\" -- Program terminating -- \")
This is the character.text file it is reading from:
Wonder Woman Diana Prince h 5 5 0 0 90 Batman Bruce Wayne h 6 2 0 4 80 The Joker Jack Napier v 5 1 0 4 80 Superman Clark Kent h 7 4 0 3 100 Catwoman Selina Kyle v 12 0 6 6 50 Aquaman Arthur Curry h 8 2 2 4 30 Iron Man Tony Stark h 10 6 3 1 50 Hulk Bruce Banner h 7 2 1 4 80 Thanos n/a v 10 2 0 8 90
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
