In: Computer Science
Homework 3 – World Series and Inpatient Payment System
This assignment has two parts: one that takes information from the web and one that takes it from a file. To select the type of information to gather, a menu will be used. Although the assignment does not require the use of functions, they can be useful.
***** This question uses Python Programming Language. The website can not be provided in the question, but the website entails every World Series since 1903, the year it was played, the team who won, the team who lost, and what the series score was. *****
MENU
The menu will have six choices.
WEB
The program will go to this website: On that page is a table showing
Gather all these statistics into one of these configurations: a single list, multiple lists, or a dictionary. Another method could be to store the information in one or more files. There is no requirement for having a certain method for storing this information. It is your choice.
See the MENU section for the required information to gather.
FILE
Open it to find this information:
The results of the Charges or the Referrals will be written to a file.
Both the Web and File sections can go in one module. You can do separate modules, if you wish.
See the attached rubric for requirements.
Rubric
Homework 3 - World Series and Inpatient Payment |
Points |
Is there a main function? |
|
Yes (5) |
5 |
Is the code inside main? |
|
Yes (5) |
5 |
Is there a menu? |
|
Yes (5) |
5 |
Does the menu work? |
|
Yes (5) |
5 |
WEB |
|
Did you get the title? |
|
Yes (5) |
5 |
Did you get the years where there were shutouts? |
|
Yes (10) |
10 |
Did you do one of the options? Winner / Loser or |
|
Yes (10) |
10 |
FILE |
|
Did you get the number of records in South Dakota? |
|
Yes (10) |
10 |
Did you do one of the options? Average Charges / |
|
Yes (10) |
10 |
Did you write the charges / referrals to a file? |
|
Yes (10) |
10 |
Did you use variables? |
|
Yes (15) |
15 |
Is standard variable naming used? |
|
Yes (5) |
5 |
Is the standard function naming used? |
|
Yes (5) |
5 |
Are all errors handled? |
|
Yes (30) max of 3 @ 10 pts. each |
30 |
Are comments used? |
|
Header (5) |
5 |
Body (10) |
10 |
Is the code written neatly? |
|
Yes (5) |
5 |
Are the results displayed neatly? |
|
Yes (5) |
5 |
Miscellaneous: anything not in the |
10 |
I will create a menu for you and the normal code the read from the file and from the web page.
The menu is as follows(Note : add the url in the line specified in the program)
import requests
from bs4 import BeautifulSoup as bs
from urllib.request import urlopen
import pandas as pd
def removeTags(s):
'''Extract the text in the html line'''
tag = False
quote = False
out = ""
for c in s:
if c == '<' and not quote:
tag = True
elif c == '>' and not quote:
tag = False
elif (c == '"' or c == "'") and tag:
quote = not quote
elif not tag:
out = out + c
return out
# TODO : Paste the url after the equal to sign in a single quotes, becase I cant submit external links in the answer
# url = '****** PASTE THE URL in the next line *********'
url = ""
html = urlopen(url).read()
data = bs(html)
table = data.find(lambda tag: tag.name=='table' and tag.has_attr('id') and tag['id']=="tablesorter-demo")
rows = table.findAll(lambda tag: tag.name=='tr')
# conert to the list
tableList = []
for j in range(1,len(rows)):
li = []
temp = rows[j].findChildren()
for i in range(5): #we only need first 4 attributs
# remove tags
data = removeTags(temp[i])
li.append(data)
tableList.append(li)
# Convert the list to the dataframe for easy computations
df = pd.DataFrame(tableList, columns =['Year', 'World SeriesWinner', 'World SeriesLoser', 'Games', 'MVP' ])
def menu():
'''This is the main menu'''
while True:
print("---------- MENU ----------")
print("1. Get title of the webpage")
print("2. Number of shoutout")
print("3. Exit")
print("4. Submenu 1")
print("5. Records from south dakota")
print("6. submenu 2")
option = int(input("Enter your option : "))
if option <1 or option >6:
print("Invalid option, Enter again")
continue
break
return option
def submenu1():
'''This is the submenu 1'''
while True:
print("---------- SUB MENU 1 ----------")
print("1. Find the winner and looser of the year")
print("2. Find the numebr of times the team wins")
option = int(input("Enter your option : "))
if option <1 or option >2:
print("Invalid option, Enter again")
continue
break
return option
def submenu2():
'''This is the submenu 2'''
while True:
print("---------- SUB MENU 1 ----------")
print("1. Highest of average covered")
print("2. Referals from ca")
option = int(input("Enter your option : "))
if option <1 or option >2:
print("Invalid option, Enter again")
continue
break
return option
def main():
while True:
option = menu()
if option == 3:
print("Thank you. Exiting the program")
break # exit condition
if option == 1:
# Get title of the webpage
# Implement the code to read from the webpage and display the title
r = requests.get(url) # request the web page
soup = bs(r.content, 'lxml') # parse it using bs4
print("Title of the webpage : ",soup.select_one('title').text)
elif option == 2:
# Number of shoutout
shoutoutCount = 0
for i in df.index:
# Check it the current game is a shoutout
if df['Games'][i] == '4-0':
shoutoutCount += 1
print(f"Number of shoutouts : {shoutoutCount}")
elif option == 4:
option = submenu1()
if option == 1:
# Find the winner and looser of the year
year = int(input("Enter the year : "))
found =False
for i in df.index:
if df['Year'][i] == str(year):
winningTeam = df['World SeriesWinner'][i]
loosingTeam = df['World SeriesLoser'][i]
found = True
break
if found:
print(f"Winning team : {winningTeam}")
print(f"Loosing team : {loosingTeam}")
else:
print("Year entered was not found on the database")
else:
# Find the numebr of times the team wins
teamName = input("Enter the team name : ")
winningCount = 0
found =False
for i in df.index:
if teamName in df['World SeriesWinner'][i]:
found = True
winningCount += 1
if found:
print(f"The team {teamName} has won {winningCount} games")
else:
print("Team Name entered was not found on the database")
elif option == 5:
print("TODO :Records from south dakota")
else :
# TODO : submenu 2
option = submenu2()
if option == 1:
print("# TODO :Highest of average covered")
else:
print("# TODO : Referals from ca")
main()
Screenshot of the output :
Screenshot of the code for understanding the indentation :
And about the case of reading from the file,
you can read each line and do your task . the code to read fromt the file is given below
f = open("filename.csv")
data = f.read() # THis line will read all the contents in the file, Now you can parse the line and do the task
f.close()
Comment below, if any doubts regarding this