Skip to content

Instantly share code, notes, and snippets.

@KKarthikeya
KKarthikeya / 2.2.py
Last active September 10, 2020 15:52
Write a program that uses raw_input to prompt a user for their name and then welcomes them. Note that raw_input will pop up a dialog box.
#Write a program that uses raw_input to prompt a user for their name and then welcomes them. Note that raw_input will pop up a dialog box. Enter Sarah in the pop-up box when you are prompted so your output will match the desired output.
name = raw_input("Enter your name:")
print "Hello",name
@KKarthikeya
KKarthikeya / 2.3.py
Created August 16, 2015 04:14
Write a program to prompt the user for hours and rate per hour using raw_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 raw_input to read a string and float() to convert the string to a number. Do not worry about error checking or bad user data.
hours = float(raw_input("Enter Hours:"))
rate = float(raw_input("Enter rate:"))
print hours*rate
@KKarthikeya
KKarthikeya / 3.1.py
Created August 16, 2015 04:15
Write a program to prompt the user for hours and rate per hour using raw_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 raw_input to read a string and…
hours = float(raw_input("Enter Hours:"))
rate = float(raw_input("Enter Pay rate per hour:"))
if hours>40.0 :
pay = rate*40
pay = pay+(1.5*rate*(hours-40))
else :
pay = rate*hours
print pay
@KKarthikeya
KKarthikeya / 3.3.py
Last active November 9, 2022 11:06
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 print A, Grade >= 0.8 print B, Grade >= 0.7 print C, Grade>= 0.6 print D, Grade < 0.6 print F If the user enters a value out of range, print a su…
score = float(raw_input("Enter score between 0.0 and 1.0: "))
if score>1.0 or score<0.0 :
print "error"
elif score>=0.9 :
print 'A'
elif score>=0.8 :
print 'B'
elif score>=0.7 :
print 'C'
elif score>=0.6 :
@KKarthikeya
KKarthikeya / 4.6.py
Created August 16, 2015 04:19
Write a program to prompt the user for hours and rate per hour using raw_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 valu…
def computepay(hours,rate):
if hours>40.0:
p = rate * 40.0
p = p+(1.5*rate*(hours-40))
else:
p = rate*hours
return p
hours = float(raw_input("Enter worked hours: "))
rate = float(raw_input("Enter Pay rate per hour: "))
print computepay(hours,rate)
@KKarthikeya
KKarthikeya / 5.2.py
Created August 16, 2015 04:21
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.
largest = None
smallest = None
while True:
num = raw_input("Enter a number:")
try:
if num == 'done':
print "Maximum is",largest
print "Minimum is",smallest
break
num = int(num)
@KKarthikeya
KKarthikeya / 6.5.py
Created August 16, 2015 04: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"
epos = text.find(':')
print float(text[epos+1:])
@KKarthikeya
KKarthikeya / 7.1.py
Created August 16, 2015 04:24
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.pythonlearn.com/code/words.txt
for data in open(raw_input("Enter file name: ")):
print data.rstrip().upper()
@KKarthikeya
KKarthikeya / 7.2.py
Created August 16, 2015 04:25
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. You can download the sample data at h…
sum = 0.0
count = 0
fname = open(raw_input("Enter file name: "))
for line in fname:
if not line.startswith("X-DSPAM-Confidence:") : continue
sum = sum + float(line[line.find(" ")+1:])
count = count + 1
print "Average spam confidence:",sum/float(count)
@KKarthikeya
KKarthikeya / 8.4.py
Created August 16, 2015 04:28
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() function. 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 …
finlist = list()
for line in open(raw_input("Enter the file Name:")):
words = line.rstrip().split()
for arr in words:
if arr in finlist:
continue
else:
finlist.append(arr)
finlist.sort()
print finlist