Skip to content

Instantly share code, notes, and snippets.

View hareom284's full-sized avatar
🔍
I do what I love.

Harry hareom284

🔍
I do what I love.
  • @leaptech.sg
  • Thailand
  • 04:17 (UTC +07:00)
  • X @hareom284
View GitHub Profile
@hareom284
hareom284 / 7.1.py
Created July 22, 2020 14:57
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.
fname = input("Enter file name: ")
try :
fh = open(fname)
except:
print(fname," files does not exit .Enter valid file names")
quit()
for line in fh :
line =line.rstrip()# for removing newline from the letter
print(line.upper())
@hareom284
hareom284 / 6.5.py
Last active July 21, 2020 18:22
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"
start = text.find('0')
end = text[start:29]
end = float(end)
print(end)
@hareom284
hareom284 / 5.2.py
Created July 17, 2020 05:29
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 match…
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == 'done':
break
try :
num = int(num)
if (largest==None) :
largest = num
@hareom284
hareom284 / 4.6.py
Created July 16, 2020 09:06
4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation.…
def computepay(h,r):
if h>40 :
return 40 * r + (h-40)*r*1.5
hrs = input("Enter Hours:")
rate = input("Enter rate")
try :
hrs = float(hrs)
rate = float(rate)
except :
@hareom284
hareom284 / 3.3.py
Last active July 16, 2020 07:43
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")
try:
score = float(score)
except :
print("Please Enter Valid Input")
quit()
if score >= 0.9 :
print("A")
elif score>=0.8:
print("B")
@hareom284
hareom284 / 3.1.py
Last active July 22, 2020 15: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…
hrs = int(input("Enter Hours:"))
rate = input("Enter rate")
rate = float(rate)
if hrs>40 :
print(rate*40 + (hrs-40)*1.5*rate)