Programming for Everybody (Getting Started with Python) -all weeks assignment solutions


These solutions are for reference only.
It is recommended that you should solve the assignment and quiz by yourself honestly then only it makes sense to complete the course.
but if you cant figure out some part of it than you can refer these solutions
make sure you understand the solution
dont just copy paste it


--------------------------------------






Week 3 - Assignment Write hello world


Write a program that uses a print statement to say 'hello world' as shown in 'Desired Output'.

Desired Output
hello world
# the code below almost works
print "hello world"
hello world






Write a program that uses input to prompt a user for their name and then welcomes them. Note that 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.

# The code below almost works

name = raw_input("Enter your name")
print "Hello", name
     Enter your nameSarah
     Hello Sarah




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.

# This first line is provided for you

hrs = float(raw_input("Enter Hours:"))
rph = float(raw_input("Enter rate per hour"))
print hrs*rph
Enter Hours:35
Enter rate per hour2.75
96.25





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 float() to convert the string to a number. 
Do not worry about error checking the user input - assume the user types numbers properly.

hrs = raw_input("Enter Hours:")
h = float(hrs)
rph = float(raw_input("Enter rate per hour"))

if hrs > 40:
    print 40*rph + (h-40)*rph*1.5
else:
    print h*rph
Enter Hours:45
Enter rate per hour10.5
498.75





 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 of 0.85.


score = float(raw_input("Enter Score: "))
if(score>1.0):
    print ("out of range")   
elif score >= 0.9:
    print ("A")
elif score >= 0.8:
    print ("B")
elif score >= 0.7:
    print ("C")
elif score >= 0.6:
    print ("D")
elif score < 0.6:
    print ("F")
      
             B





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.

The function should return a value. 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 float() to convert the string to a number. 

Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. 

Do not name your variable sum or use the sum() function.


def computepay(h,r):
    if h>40:
        rate1 = (r*1.5)*(h-40)
    return ((h-5)*r)+rate1
hrs = float(raw_input("Enter Hours:"))
rate = float(raw_input("Enter Rate:"))
p = computepay(hrs,rate)
print (p)

498.75





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 the output below.


largest = None
smallest = None
while True:
    num = raw_input("Enter a number: ")
    if num == "done": break
    try:
        num = int(num)
        if largest is None or largest < num: largest = num
        if smallest is None or smallest > num: smallest = num
    except:
        print ("Invalid input")
        continue
print ("Maximum is",largest)
print ("Minimum is",smallest)
Invalid input Maximum is 10 Minimum is 2



reference :coursera.org

No comments

darkmode