This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main.
def file_to_dictionary(rosterFile):
myDictionary={}
with open(rosterFile,'r') as f:
data=f.read().split('\n')
for line in data:
(num,first,last,position)=line.split()
myDict=[first, last, position]
myDictionary[num]=myDict
print (myDictionary)
return myDictionary
file_to_dictionary((f"../data/playerRoster.txt"))
def find_by_number(number):
player=None
second=[]
foundplayer= False
myDictionary=file_to_dictionary((f"../data/playerRoster.txt"))
for p in myDictionary:
fullplayer=p.split()
second.append([fullplayer[0], (fullplayer[1]+" "+ fullplayer[2]+" "+fullplayer[3])])
for people in second:
if int(people[0]) == number:
player= people[1]
return player
def main():
x=0
while x!=-1:
x=int(input("Enter a number for lookup. Or enter -1 to quit."))
if find_by_number(x) == None and x !=-1:
print("There is no one with that number...sorry.")
elif find_by_number(x) != None and x != -1:
print (find_by_number(x))
elif x==-1:
print("okay goodbye.")
main()
Short Summary:
Have made some changes and corrected the errors in the code
Have created a sample array and written a file playerRoaster.txt and updated the file path for testing. Please ignore the same and update your file path accordingly.
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
#Line number 2 to 7 (below 4 lines) is for testing purpose,
please ignore it
L = ["10 Tom Cruise 1\n", "11 John Abraham 2\n", "12 Judith Ram
7\n"]
# writing to file
file1 = open('playerRoster.txt', 'w')
file1.writelines(L)
file1.close()
def file_to_dictionary(rosterFile):
myDictionary={}
with open(rosterFile,'r') as f:
data=f.read().split('\n')
#Include this to remove empty lines
data.remove("")
for line in data:
#(num,first,last,position)=line.split()
#Assign to a list and then create the vars
list1=line.split()
num=list1[0]
first=list1[1]
last=list1[2]
position=list1[3]
myDict=[first, last, position]
myDictionary[num]=myDict
print (myDictionary)
return myDictionary
file_to_dictionary(("playerRoster.txt"))
def find_by_number(number):
player=None
second=[]
foundplayer= False
myDictionary=file_to_dictionary(("playerRoster.txt"))
for p in myDictionary:
#Commenting the below lines as it's wrong way of accessing a
dictionery and not required
#fullplayer=p.split()
#second.append([fullplayer[0], (fullplayer[1]+" "+ fullplayer[2]+" "+fullplayer[3])])
#for people in second:
#if int(people[0]) == number:
if int(p) == number:
#player= people[1]
player=myDictionary[p]
return player
def main():
x=0
while x!=-1:
x=int(input("Enter a number for lookup. Or enter -1 to quit."))
if find_by_number(x) == None and x !=-1:
print("There is no one with that number...sorry.")
elif find_by_number(x) != None and x != -1:
print (find_by_number(x))
elif x==-1:
print("okay goodbye.")
main()
Code Screenshot:
Output:
**************Please do upvote to appreciate our time.
Thank you!******************
Get Answers For Free
Most questions answered within 1 hours.