;Python: * Save additionWeb.py or skeletonForWeb.py as quotientWeb.py. Modify it
ID: 3849451 • Letter: #
Question
;Python:
* Save additionWeb.py or skeletonForWeb.py as quotientWeb.py. Modify it to display the results of a division problem in a web page. As in the exercises in Chapter 1, display a full sentence labeling both the integer quotient and the remainder. You can take your calculations from Quotient String Return Exercise. You should only need to make Python changes to the processInput and main functions. You will also need the HTML for the page displayed. Make a web page template file called quotientTemplate.html and read it into your program.
additionWeb.py:
'''Prompt the user for two integers and display a web page with the sum.'''
def processInput(numStr1, numStr2): # NEW
'''Process input parameters and return the final page as a string.'''
num1 = int(numStr1) # transform input to output data
num2 = int(numStr2)
total = num1+num2
return fileToStr('additionTemplate.html').format(**locals())
def main(): # NEW
numStr1 = input('Enter an integer: ') # obtain input
numStr2 = input('Enter another integer: ')
contents = processInput(numStr1, numStr2) # process input into a page
browseLocal(contents) # display page
def fileToStr(fileName):
"""Return a string containing the contents of the named file."""
fin = open(fileName);
contents = fin.read();
fin.close()
return contents
def strToFile(text, filename):
"""Write a file with the given name and the given text."""
output = open(filename,"w")
output.write(text)
output.close()
def browseLocal(webpageText, filename='tempBrowseLocal.html'):
'''Start your webbrowser on a local file containing the text
with given filename.'''
import webbrowser, os.path
strToFile(webpageText, filename)
webbrowser.open("file:///" + os.path.abspath(filename)) #elaborated for Mac
main()
Explanation / Answer
divsionTemplate.html: (keep in same folder)
<html>
<head>
<title>some title</title>
</head>
<body>
<h1>You tried dividing {num1} by {num2}</h1>
<h3>Quotient: {q}</h1>
<h3>Remainder: {r}</h1>
</body>
</html>
quotientWeb.py:
'''Prompt the user for two integers and display a web page with the sum.'''
def processInput(numStr1, numStr2): # NEW
'''Process input parameters and return the final page as a string.'''
num1 = int(numStr1) # transform input to output data
num2 = int(numStr2)
q = num1//num2
r = num1%num2
return fileToStr('divisionTemplate.html').format(**locals())
def main(): # NEW
numStr1 = input('Enter an integer: ') # obtain input
numStr2 = input('Enter another integer: ')
contents = processInput(numStr1, numStr2) # process input into a page
browseLocal(contents) # display page
def fileToStr(fileName):
"""Return a string containing the contents of the named file."""
fin = open(fileName);
contents = fin.read();
fin.close()
return contents
def strToFile(text, filename):
"""Write a file with the given name and the given text."""
output = open(filename,"w")
output.write(text)
output.close()
def browseLocal(webpageText, filename='tempBrowseLocal.html'):
'''Start your webbrowser on a local file containing the text
with given filename.'''
import webbrowser, os.path
strToFile(webpageText, filename)
webbrowser.open("file:///" + os.path.abspath(filename)) #elaborated for Mac
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.