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);...
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()...
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...
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...
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 =...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in...
*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in a common pool b) The goal of the game is to take the last coin c) Players take turns removing 1, 2, or 3 coins from the pool d) A player cannot take more coins than remain in the pool e) After each turn, the number of coins that remain in the pool is announced f) When the last coin is taken, the winner...
This is an example out of my text which I copied directly but am getting an...
This is an example out of my text which I copied directly but am getting an error every iteration and am unsure how to correct it. The error is " the condition has length > 1 and only the first element will be used" and my code is: f <- function(x, sigma) { if (any(x < 0)) return(0) stopifnot(sigma > 0) return((x/sigma^2)*exp(-x^2 / (s*sigma^2))) } xt <- x[i-1] y <- rchisq(1, df = xt) m <- 10000 sigma <- 4...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT