Question

This is my code, python. I have to search through the roster list to find a...

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()

Homework Answers

Answer #1

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!******************

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following questions using the list below. You can record your answers in the essay textbox. data = [5,3,7] A. Write code to replace the value in the 0 position with the number 8. B. Add the value 10 to the end of the list. C. Insert the value 22 after position 1 in the list. D. Remove the value at position 2. E. Sort the...
i need this code to print a number of stars depending on how much the user...
i need this code to print a number of stars depending on how much the user wants and after * it prints no stars. if the user enters a negative number it should print "error invalid number" this is my code so far: def stars(n,i): stars(n, 1) if n <= 0: return "No stars" if i <= n: print("* ", end="") stars(n, i + 1) else: print("no stars") stars(n - 1, 1) n = int(input("enter an integer")) def main(): stars()...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
How do I fix my code to collect ONLY hashtag instead of accumulating all the information...
How do I fix my code to collect ONLY hashtag instead of accumulating all the information about the number of occurrences of all words from the textfile? Please help to fix below codes. tweetcount=0 maxcount=0 count = 0 with open('elon-musk.txt') as book: for tweet in book: count += 1 print("Number of tweets:", count) print() with open('elon-musk.txt') as book: for line in book: s count = len(line.split()) if count>maxcount: maxline = line maxcount = count tweetcount += 1 print("Tweet with max...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT