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_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_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_05_02
Created October 7, 2017 12:44
5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and m…
largest = None
smallest = None
the_list = []
while True:
num = input()
if num == "done":break
if len(num) < 1: break
try:
number = int(num)
@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_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_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_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_08_05
Created October 7, 2017 12:54
8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line: From [email protected] Sat Jan 5 09:14:16 2008 You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out…
fhand = open("mbox-short.txt")
count = 0
for line in fhand:
line = line.rstrip()
if line == "": continue
words = line.split()
if words[0] !="From": continue
print(words[1])