Skip to content

Instantly share code, notes, and snippets.

View MichelleDalalJian's full-sized avatar

Michelle Dalal Jian MichelleDalalJian

View GitHub Profile
@MichelleDalalJian
MichelleDalalJian / py4e_ex_02_03
Created October 7, 2017 12:38
2.3 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking or bad user data.
xh = input("Enter Hours:")
xr = input("Enter Rate:")
xp = float(xh)*float(xr)
print("Pay:",xp)
@MichelleDalalJian
MichelleDalalJian / py4e_ex_03_01
Created October 7, 2017 12:39
3.1 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and floa…
xh = input("Enter Hours:")
xr = input ("Enter Rate:")
fh = float (xh)
fr = float (xr)
if fh > 40:
reg = fh*fr
otp = (fh-40)*(fr*0.5)
xp = reg + otp
else:
@MichelleDalalJian
MichelleDalalJian / py4e_ex_04_06
Created October 7, 2017 12:43
4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Award time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of time-and-a-half in a function called computepay() and use the function to do the computation. The function should return a value…
def computepay(hours, rate):
xh=float(hours)
xr=float(rate)
if xh < 40:
pay = xh*xr
return pay
else:
pay=(xr*40)+(xh-40)*(xr*1.50)
return pay
@MichelleDalalJian
MichelleDalalJian / py4e_ex_07_01
Created October 7, 2017 12:51
7.1 Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below. You can download the sample data at http://www.py4e.com/code3/words.txt
# Use words.txt as the file name
input("Enter File Name")
fhand = open ("words.txt")
for line in fhand:
line = line.rstrip()
line = line.upper()
print(line)
@MichelleDalalJian
MichelleDalalJian / py4e_ex_08_04
Created October 7, 2017 12:53
8.4 Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in…
fhand = open("romeo.txt")
lst = list()
for line in fhand:
line = line.rstrip()
line = line.split()
for i in line:
if i not in lst:
lst.append(i)
@MichelleDalalJian
MichelleDalalJian / py4e_ex_06_05
Created October 7, 2017 12:50
6.5 Write code using find() and string slicing (see section 6.10) to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out.
text = "X-DSPAM-Confidence: 0.8475";
x = text.find(" ")
y = text[x:]
fy=float(y)
print(fy)
@MichelleDalalJian
MichelleDalalJian / py4e_ex_07_02
Created October 7, 2017 12:52
7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or …
# Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fhand = open(fname)
count = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:") :
count = count + 1
total = 0
@MichelleDalalJian
MichelleDalalJian / py4e_ex_03_03
Created October 7, 2017 12:41
3.3 Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table: Score Grade >= 0.9 A >= 0.8 B >= 0.7 C >= 0.6 D < 0.6 F If the user enters a value out of range, print a suitable error message and exit. For the test, enter a …
score = input("Enter Score: ")
x = float(score)
if x >=0.9:
print("A")
elif x >=0.8:
print("B")
elif x >=0.7:
print("C")
@MichelleDalalJian
MichelleDalalJian / py4e_ex_12
Created October 7, 2017 14:53
Exploring the HyperText Transport Protocol You are to retrieve the following document using the HTTP protocol in a way that you can examine the HTTP Response headers. http://data.pr4e.org/intro-short.txt There are three ways that you might retrieve this web page and look at the response headers: Preferred: Modify the socket1.py program to retrie…
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)
while True:
data = mysock.recv(512)
if (len(data) < 1):
@MichelleDalalJian
MichelleDalalJian / py4e_ex_12_02
Created November 24, 2017 16:04
Following Links in Python: The program will use urllib to read the HTML from the data files below, extract the href= vaues from the anchor tags, scan for a tag that is in a particular position relative to the first name in the list, follow that link and repeat the process a number of times and report the last name you find.
from bs4 import BeautifulSoup
import urllib.request, urllib.parse, urllib.error
import ssl
import re
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = "http://py4e-data.dr-chuck.net/known_by_Bryce.html"