【Programming for Everybody】学习笔记

Programming for Everybody

(Getting Started with Python)

by University of Michigan

Charles Severance



Download TextWrangler

http://www.barebones.com/products/textwrangler/

Use TextWrangler to Run in Terminal

【Programming for Everybody】学习笔记_第1张图片


Run in Terminal

【Programming for Everybody】学习笔记_第2张图片


Exercises:

2.2 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.

# Desired Output
# Hello Sarah

name = raw_input("What's your name?")
print "Hello "+name

2.3 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.

# Desired Output
# 96.25

hrs = raw_input("Enter Hours:")
rate = raw_input("Enter rate:")
result = float(rate)*float(hrs)
print result

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

# Input: hrs=45, rate=10.5
# Desired Output: 498.75

hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter Rate:")
r = float(rate)

if h>40:
    result = 40*r+(h-40)*r*1.5
else:
    result = h*r

print result


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

# Input: 0.85
# Desired Output: B

score = raw_input("what's the score:")
s = float(score)

if s<0.0 or s>1.0:
    print "error"
elif s<0.6:
    print "F"
elif s<0.7:
    print "D"
elif s<0.8:
    print "C"
elif s<0.9:
    print "B"
else:
    print "A"

4.6 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 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 raw_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.

# Input: hrs=45, rate=10.5
# Output: 498.75

def computepay(h,r):
    
    if h<=40:
        return h*r
    else:
    	return 40*r+(h-40)*r*1.5

hrs = raw_input("Enter Hours:")
rate = raw_input("Enter Rate:")

hr = float(hrs)
ra = float(rate)

p = computepay(hr,ra)

print p


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 the numbers from the book for problem 5.1 and Match the desired output as shown.

# Input: 5,6,hehe,4,7
# Desired Output: 
#     Invalid input
#     Maximum is 7
#     Minimum is 4


largest = None
smallest = None

while True:
    num = raw_input("Enter a number: ")
    if num == "done" : break
        
    try:
        n=int(num)

    except:
        print "Invalid input"
            
    if n>largest:
        largest=n
    if smallest is None or n




你可能感兴趣的:(coursera-python)