01 Python Test
在Codeacademy上学习Python课程,刷题的笔记记录如下,欢迎交流!
Practice Makes Perfact
Fun With Numbers
1. Practice Practice Practice!
题目001:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三>位数?都是多少?
方法1:
num=[1,2,3,4]
for i in num:
for j in num:
if j!=i:
for k in num:
if (k!=i and k!=j):
total =0
total =i*100+j*10+k
print total
方法2:
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if( i != k ) and (i != j) and (j != k):
print i,j,k
ii. What’s the Score? 2/9
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
iii. Put It Together 3/9
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = [lloyd, alice, tyler]
iv. For the Record 4/9
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = [lloyd, alice, tyler]
for x in students:
print x['name']
print x['homework']
print x['quizzes']
print x['tests']
Just Average
iv. It’s Okay to be Average 5/9
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
# Add your function below!
def average(numbers):
total = sum (numbers)
aver = float(total)/len(numbers)
return aver
ii. Just Weight and See 6/9
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
# Add your function below!
def average(numbers):
total = sum (numbers)
aver = float(total)/len(numbers)
return aver
def get_average(student):
homework= average(student["homework"])
quizzes =average(student["quizzes"])
tests = average(student["tests"])
return 0.1*homework + 0.3*quizzes + 0.6*tests
iii. Sending a Letter 7/9
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
# Add your function below!
def average(numbers):
total = sum (numbers)
aver = float(total)/len(numbers)
return aver
def get_average(student):
homework= average(student["homework"])
quizzes =average(student["quizzes"])
tests = average(student["tests"])
return 0.1*homework + 0.3*quizzes + 0.6*tests
def get_letter_grade(score):
if 90<=score:
return "A"
elif 80<=score<90:
return "B"
elif 70<=score<80:
return "C"
elif 60<=score<70:
return "D"
else:
return "F"
get_average(lloyd)
iv. Part of the Whole 8/9
#列表如何作为函数的入口参数
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
# Add your function below!
def average(numbers):
total = sum (numbers)
aver = float(total)/len(numbers)
return aver
def get_average(student):
homework= average(student["homework"])
quizzes =average(student["quizzes"])
tests = average(student["tests"])
return 0.1*homework + 0.3*quizzes + 0.6*tests
def get_letter_grade(score):
if 90<=score:
return "A"
elif 80<=score<90:
return "B"
elif 70<=score<80:
return "C"
elif 60<=score<70:
return "D"
else:
return "F"
students=["lloyd","alice","tyler"]
def get_class_average(students):
results=[]
for student in students:
results.append(get_average(student))
return average(results)
10. censor
#方法1
def censor(text, word):
x = text.split()
for i in range(len(x)):
if x[i] == word:
x[i] = "*" * len(word)
return " ".join(x)
#方法2
def censor(text, word):
if word in text:
text = text.replace(word,"*"*len(word))
return text