Reputation: 45
I am very new to coding so this is all I have right now. I am running into problems trying to select the highest number and printing out the word that corresponds with it.
Edit: I am also supposed to write a code that also ignores the '#'
character and anything that comes after it.
text = open('heroes.txt', 'r')
for line in sorted(text, key = lambda line: line.split()[1]):
if not '#' in line:
line.isalnum() or line == '_'
continue
This is the text file with the lists
Upvotes: 0
Views: 61
Reputation: 172
Based off SuperStew's answer, changed to make it a little more intuitive for a beginner. Added comments.
text = open('heroes.txt', 'r')
# Empty dictionary
data = {}
# For every line in the file...
for line in text.readlines():
# skip if line starts with #
if line.startswith('#'):
continue
# Split the name into two parts, hero name and hero power.
row = line.split()
# Map row data to dictionary. Hero name will be key, hero power will be value.
data[row[0]] = row[1]
# Remember to close the file
text.close()
# For every key value pair in data...
for k,v in data.items():
# See if value is the max value. If so, print it.
if v==max(data.values()):
print(k)
Useful links:
https://www.w3schools.com/python/ref_string_split.asp
https://www.w3schools.com/python/python_dictionaries.asp
Upvotes: 1
Reputation: 3054
this might do the trick
text = open('heroes.txt', 'r')
data={}
for line in text.readlines():
if line.startswith('#'):
continue
d_l=line.split()
d_l=[[x.strip() for x in y] for y in d_l]
data[d_l[0]]= d_l[1]
text.close() #remember to close file
for k,v in data.items():
if v==max(data.values()):
print(k)
Upvotes: 1