Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.25 KB | None | 0 0
  1. import sys
  2. import random
  3.  
  4. def main():
  5.  
  6.     #Menu
  7.     try:
  8.         x = True
  9.         while (x == True):
  10.             print("If you would like to import a file, press 1,")
  11.             print("If you would like to make a file, press 2,")
  12.             print("If you would like to search player stats, press 3,")
  13.             print("If you would like to quit, press 4.\n")
  14.             choice = '0'
  15.             choice = input("Enter your choice of a number between 1 and 4: ")
  16.  
  17.             while choice != '1' and choice != '2' and choice != '3' and choice != '4':
  18.                 choice = input("Enter your choice of a number between 1 and 4: ")
  19.             if choice == '1':
  20.                 inData = read_in()
  21.                 headerLine = inData[0]
  22.                 manipulation = data_process(inData)
  23.                 write_out(manipulation, headerLine)
  24.                 print("Data processed")
  25.             elif choice == '2':
  26.                 input_team()
  27.             elif choice == '3':
  28.                 output_data(search_player())
  29.             elif choice == '4':
  30.                 x = False
  31.             else:
  32.                 break
  33.     except ValueError:
  34.         choice = input("Enter your choice of a number between 1 and 4: ")
  35.  
  36.  
  37. def read_in():
  38.     try:
  39.         fileName = input("Please enter the full name of the file you'd like to import: ")
  40.         fr = open(fileName, "r")
  41.         inData = []
  42.         line = fr.readline()
  43.        
  44.         while line != "":
  45.             inData.append(line)
  46.             line = fr.readline()
  47.            
  48.            
  49.         fr.close()
  50.         #print(inData)
  51.         return inData
  52.    
  53.     except FileNotFoundError:
  54.         print("Could not find the correct file.")
  55.         sys.exit()
  56.  
  57. def data_process(inData):
  58.     index = 0
  59.     while index < len(inData):
  60.         inData[index] = inData[index].strip(",\n")
  61.         index += 1
  62.        
  63.     index = 1
  64.     names = []
  65.     items= []
  66.     manipulation = []
  67.     x = 0
  68.  
  69.     #we also need to strip the quotation marks
  70.     while index < len(inData):
  71.         items = inData[index].split(",")
  72.         while x < len(items):
  73.             if x == 0:
  74.                
  75.                 names = items[x].split(' ')
  76.                 FNAME = names[0].strip('"')
  77.                 LNAME = names[1].strip('"')
  78.                 if len(names) == 3:
  79.                     #adds paula to de paula, keeps the space for filtering later
  80.                     LNAME += " " + names[2].strip('"')
  81.                 #print(FNAME+","+LNAME) #debug
  82.                 #print(items[0])
  83.                 del items[0]
  84.                 items.insert( 0, FNAME) #these three lines delete the "Fname, Lname", and replace it with 2 seperate entries
  85.                 items.insert( 1, LNAME)
  86.  
  87.             else:
  88.                 items[x] = items[x].strip('"')
  89.            
  90.             if x == 4: #At Bats
  91.                 ab = int(items[x])
  92.                 #print(ab)
  93.             if x == 6: #Hits
  94.                 h = int(items[x])
  95.                 #print(h)
  96.             x+=1
  97.         try:
  98.             avg = format((h/ab),'.3f')
  99.             items.append(str(avg))
  100.         except ZeroDivisionError:
  101.             avg = 0.0
  102.             items.append(str(avg))
  103.  
  104.         #print(avg) #debug
  105.         #items.append(str(avg))
  106.  
  107.         #items.append("\n")
  108.         #print(items) #debug
  109.         items = " ".join(items)
  110.         #print(items)
  111.         manipulation.append(items)
  112.  
  113.         #this resets the line back to the first item, then jump down a line
  114.         x = 0
  115.         index +=1
  116.    
  117.     #manipulation = inData
  118.     #print(manipulation) #debug    
  119.     return(manipulation)
  120.  
  121. def write_out(manipulation, headerLine):
  122.  
  123.     #manipulation += "\n"
  124.     fw = open('NyStats.msst', 'w')
  125.     headerLine = headerLine.replace('","',' ')
  126.     headerLine = headerLine.replace('"','')
  127.     headerLine = headerLine.replace('Name','FNAME LNAME')
  128.     headerLine = headerLine.strip('\n') + " Avg \n"
  129.     fw.write(headerLine)
  130.    
  131.     x = 0
  132.     #placeholder = str(manipulation[x]).strip("','")
  133.     for x in manipulation:
  134.         #print(x) #debug
  135.         fw.write(str(x) + "\n")
  136.     #while x <
  137.    
  138.     fw.close()
  139.  
  140.  
  141.  
  142. #Search
  143. def search_player():    #Done
  144.     try:
  145.         file = input("Input the name of the file you would like to search: ")
  146.         file += ".msst"
  147.         searchID = input("Input player ID: ")
  148.         searchfile = open(file,"r")
  149.         for line in searchfile:
  150.             if searchID in line:
  151.                 searched = line
  152.                 break
  153.             else:
  154.                 searched = "Player not found"
  155.         searchfile.close()
  156.         return(searched) #Seared Searching.
  157.     except FileNotFoundError:
  158.         file = print("The file was not found")
  159.         main()
  160.  
  161. def output_data(search): #Done
  162.     if search == "Player not found":
  163.         print(search)
  164.     else:
  165.         data = []
  166.         data = search.split(" ")
  167.         print(data[0],data[1],"AB:", data[4], "H:", data[6], "Avg:", data[24])
  168.         #Prints FMANE, LNAME, AB, H, avg
  169.  
  170.  
  171. def menu(manipulation, headerLine):
  172.     try:
  173.         x = True
  174.         while (x == True):
  175.             print("If you would like to import a file, press 1,")
  176.             print("If you would like to make a file, press 2,")
  177.             print("If you would like to search player stats, press 3,")
  178.             print("If you would like to quit, press 4.")
  179.             choice = '0'
  180.             choice = input("Enter your choice of a number between 1 and 4: ")
  181.  
  182.             while choice != '1' and choice != '2' and choice != '3' and choice != '4':
  183.                 choice = input("Enter your choice of a number between 1 and 4: ")
  184.             if choice == '1':
  185.                 read_in()
  186.             elif choice == '2':
  187.                 write_out(manipulation, headerLine)
  188.             elif choice == '3':
  189.                 data_process(inData)
  190.             elif choice == '4':
  191.                 x = False
  192.             else:
  193.                 break
  194.     except ValueError:
  195.         choice = input("Enter your choice of a number between 1 and 4: ")
  196.  
  197.  
  198. def input_team():
  199.     try:
  200.         fileName = input("Please enter the name of the file you'd like to add this to: ")
  201.         fileName += '.msst'
  202.         f = open(fileName, 'a')
  203.     except FileNotFoundError:
  204.         print("Unable to find file. Closing.")
  205.         sys.exit
  206.  
  207.     with open(fileName) as FILE:
  208.         FILE.seek(0)
  209.         firstcharactercheck = FILE.read(1)
  210.         if not firstcharactercheck:
  211.             print("File is empty, adding header file...")
  212.             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')
  213.         else:
  214.             FILE.seek(0)
  215.  
  216.     x = 1
  217.  
  218.     while x == 1:
  219.         print("Please enter the stats for the player")
  220.         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"
  221.         #FNAME 0 LNAME 1 AGE 2 G 3 AB 4 PA 5
  222.         headerLine = headerLine.split(" ")
  223.  
  224.         item = 0
  225.         entries = []
  226.  
  227.         while item < (len(headerLine) - 3):
  228.  
  229.             entry2 = input(headerLine[item] + ":")
  230.             entries.append(entry2)
  231.             item += 1
  232.  
  233.         dol = "0"
  234.         playerid = random.randint(20000,25000)
  235.         try:
  236.             avg = float(entries[4]) / float(entries[6])
  237.         except ZeroDivisionError:
  238.             avg = 0
  239.         except ValueError:
  240.             print("Please use numbers for At Bats, and Hits. Ending program.")
  241.             input()
  242.             sys.exit()
  243.         entries.append(str(dol))
  244.         entries.append(str(playerid))
  245.         entries.append(str(format(avg,'.3f')))
  246.         #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)]
  247.         #print(customPlayer)
  248.         customPlayer = ' '.join(entries)
  249.         customPlayer += "\n"
  250.         #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
  251.         #again and make more players. Taht Taht Taht.
  252.  
  253.         f.write(customPlayer)
  254.  
  255.         answer = input("Would you like to create a new player? Y/N ")
  256.         if answer == 'Y' or answer == 'y':
  257.             pass
  258.         else:
  259.             x = 0
  260.  
  261.     f.close
  262.  
  263.  
  264.  
  265.  
  266. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement