Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import random
- def main():
- #Menu
- try:
- x = True
- while (x == True):
- print("If you would like to import a file, press 1,")
- print("If you would like to make a file, press 2,")
- print("If you would like to search player stats, press 3,")
- print("If you would like to quit, press 4.\n")
- choice = '0'
- choice = input("Enter your choice of a number between 1 and 4: ")
- while choice != '1' and choice != '2' and choice != '3' and choice != '4':
- choice = input("Enter your choice of a number between 1 and 4: ")
- if choice == '1':
- inData = read_in()
- headerLine = inData[0]
- manipulation = data_process(inData)
- write_out(manipulation, headerLine)
- print("Data processed")
- elif choice == '2':
- input_team()
- elif choice == '3':
- output_data(search_player())
- elif choice == '4':
- x = False
- else:
- break
- except ValueError:
- choice = input("Enter your choice of a number between 1 and 4: ")
- def read_in():
- try:
- fileName = input("Please enter the full name of the file you'd like to import: ")
- fr = open(fileName, "r")
- inData = []
- line = fr.readline()
- while line != "":
- inData.append(line)
- line = fr.readline()
- fr.close()
- #print(inData)
- return inData
- except FileNotFoundError:
- print("Could not find the correct file.")
- sys.exit()
- def data_process(inData):
- index = 0
- while index < len(inData):
- inData[index] = inData[index].strip(",\n")
- index += 1
- index = 1
- names = []
- items= []
- manipulation = []
- x = 0
- #we also need to strip the quotation marks
- while index < len(inData):
- items = inData[index].split(",")
- while x < len(items):
- if x == 0:
- names = items[x].split(' ')
- FNAME = names[0].strip('"')
- LNAME = names[1].strip('"')
- if len(names) == 3:
- #adds paula to de paula, keeps the space for filtering later
- LNAME += " " + names[2].strip('"')
- #print(FNAME+","+LNAME) #debug
- #print(items[0])
- del items[0]
- items.insert( 0, FNAME) #these three lines delete the "Fname, Lname", and replace it with 2 seperate entries
- items.insert( 1, LNAME)
- else:
- items[x] = items[x].strip('"')
- if x == 4: #At Bats
- ab = int(items[x])
- #print(ab)
- if x == 6: #Hits
- h = int(items[x])
- #print(h)
- x+=1
- try:
- avg = format((h/ab),'.3f')
- items.append(str(avg))
- except ZeroDivisionError:
- avg = 0.0
- items.append(str(avg))
- #print(avg) #debug
- #items.append(str(avg))
- #items.append("\n")
- #print(items) #debug
- items = " ".join(items)
- #print(items)
- manipulation.append(items)
- #this resets the line back to the first item, then jump down a line
- x = 0
- index +=1
- #manipulation = inData
- #print(manipulation) #debug
- return(manipulation)
- def write_out(manipulation, headerLine):
- #manipulation += "\n"
- fw = open('NyStats.msst', 'w')
- headerLine = headerLine.replace('","',' ')
- headerLine = headerLine.replace('"','')
- headerLine = headerLine.replace('Name','FNAME LNAME')
- headerLine = headerLine.strip('\n') + " Avg \n"
- fw.write(headerLine)
- x = 0
- #placeholder = str(manipulation[x]).strip("','")
- for x in manipulation:
- #print(x) #debug
- fw.write(str(x) + "\n")
- #while x <
- fw.close()
- #Search
- def search_player(): #Done
- try:
- file = input("Input the name of the file you would like to search: ")
- file += ".msst"
- searchID = input("Input player ID: ")
- searchfile = open(file,"r")
- for line in searchfile:
- if searchID in line:
- searched = line
- break
- else:
- searched = "Player not found"
- searchfile.close()
- return(searched) #Seared Searching.
- except FileNotFoundError:
- file = print("The file was not found")
- main()
- def output_data(search): #Done
- if search == "Player not found":
- print(search)
- else:
- data = []
- data = search.split(" ")
- print(data[0],data[1],"AB:", data[4], "H:", data[6], "Avg:", data[24])
- #Prints FMANE, LNAME, AB, H, avg
- def menu(manipulation, headerLine):
- try:
- x = True
- while (x == True):
- print("If you would like to import a file, press 1,")
- print("If you would like to make a file, press 2,")
- print("If you would like to search player stats, press 3,")
- print("If you would like to quit, press 4.")
- choice = '0'
- choice = input("Enter your choice of a number between 1 and 4: ")
- while choice != '1' and choice != '2' and choice != '3' and choice != '4':
- choice = input("Enter your choice of a number between 1 and 4: ")
- if choice == '1':
- read_in()
- elif choice == '2':
- write_out(manipulation, headerLine)
- elif choice == '3':
- data_process(inData)
- elif choice == '4':
- x = False
- else:
- break
- except ValueError:
- choice = input("Enter your choice of a number between 1 and 4: ")
- def input_team():
- try:
- fileName = input("Please enter the name of the file you'd like to add this to: ")
- fileName += '.msst'
- f = open(fileName, 'a')
- except FileNotFoundError:
- print("Unable to find file. Closing.")
- sys.exit
- with open(fileName) as FILE:
- FILE.seek(0)
- firstcharactercheck = FILE.read(1)
- if not firstcharactercheck:
- print("File is empty, adding header file...")
- f.write('FNAME LNAME Age G AB PA H 1B 2B 3B HR R RBI BB SO HBP GDP SB CS SLG OPS WAR Dol playerid Avg\n')
- else:
- FILE.seek(0)
- x = 1
- while x == 1:
- print("Please enter the stats for the player")
- headerLine = "FNAME LNAME Age G AB PA H 1B 2B 3B HR R RBI BB SO HBP GDP SB CS SLG OPS WAR Dol playerid Avg"
- #FNAME 0 LNAME 1 AGE 2 G 3 AB 4 PA 5
- headerLine = headerLine.split(" ")
- item = 0
- entries = []
- while item < (len(headerLine) - 3):
- entry2 = input(headerLine[item] + ":")
- entries.append(entry2)
- item += 1
- dol = "0"
- playerid = random.randint(20000,25000)
- try:
- avg = float(entries[4]) / float(entries[6])
- except ZeroDivisionError:
- avg = 0
- except ValueError:
- print("Please use numbers for At Bats, and Hits. Ending program.")
- input()
- sys.exit()
- entries.append(str(dol))
- entries.append(str(playerid))
- entries.append(str(format(avg,'.3f')))
- #entries = [fname,lname,age,g,str(ab),pa,str(h),oneB,twoB,threeB,hr,r,rbi,bb,so,hbp,gdp,sb,cs,slg,ops,war,dol,str(playerid),str(avg)]
- #print(customPlayer)
- customPlayer = ' '.join(entries)
- customPlayer += "\n"
- #now I append taht stuff to a new file. That uses the append mode of the readfile command. Then, we ask the user if they'd like to run it
- #again and make more players. Taht Taht Taht.
- f.write(customPlayer)
- answer = input("Would you like to create a new player? Y/N ")
- if answer == 'Y' or answer == 'y':
- pass
- else:
- x = 0
- f.close
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement