In: Computer Science
File has a format Name and number, the number represents power. The name and the (integer) power are separated by some amount of space. Importantly, any line that begins with a hash '#' are comments and they need to be ingored. Write a program that reads from that file, and prints out only the name of the hero with the strongest power. That name should be capitalized (not uppercase, but capitalized, as in 'Galadriel')
Here is the heroes.txt
# DC heroes
# format: "name" "power"
# 57
# 83
hal 12
batman 48
grayson 14
cyclone 24
superman 38
luthor 15
joker 18
drake 33
wayne 42
rayner 18
# below heroes are additional heroes
arrow 22
kord 48
batwoman 37
supergirl 49
stargirl 24
darkseid 41
gardner 28
pennyworth 27
west 12
aquaman 47
kallor 45
arisia 36
What i have so far:
fn = open('heroes.txt')
count = 0
for z in fn:
line = z.strip()
if not '#' in line:
print(line.capitalize())
continue
po1 = line.find()
print(po1)
fn.close
# do comment if any problem arises
# Code
fn = open('heroes.txt')
# list of names of heroes
heroes = []
# list of powers of heroes
powers = []
count = 0
# this variable stores index containing max power
max_power = 0
for z in fn:
line = z.strip()
if not '#' in line:
# split name and power
name, power = line.split()
name=name.capitalize()
# add to heroes
heroes.append(name)
power = int(power)
# add to powers
powers.append(power)
# if current max power is less than current power
if powers[max_power] < power:
max_power = count
# increment count
count += 1
continue
print(heroes[max_power])
fn.close
Output: