1到4题比较简单,这里省略
5题6题代码
def main():
prompt = input("Enter the name: ").lower().split()
P = ""
num = 0
for ch in prompt:
P = P + ch
for p in P:
num = num + ord(p) - 96
print(num)
main()
7题8题答案
def main():
prompt = input("Enter the word: ").split()
s = []
key = int(input("Key: "))
for p in prompt:
q = []
for i in range(len(p)):
if ord(p[i]) + key <= ord("z"):
q.append(chr(ord(p[i]) + key))
else:
q.append(chr(ord(p[i]) + key - 26))
h = "".join(q)
s.append(h)
for ch in s:
print(ch, end=" ")
main()
9题答案
def main():
prompt=input("Enter the word: ").split()
i = 0
for p in prompt:
i=i+1
print("The number of the words is {0}.".format(i))
main()
10题答案
def main():
A = input("Please enter the words: ").split()
n = 0
totalLength = 0
for a in A:
n = n + 1
totalLength = totalLength+len(a)
avg = j / i
print("The average length is {0:0.3f}.".format(avg))
main()
11题答案
def main():
print("This program illlustrates a chaotic function")
x1, x2 = eval(input("Enter two numbers between 0 and 1 seperated by a comma: "))
n = int(input("Enter the change times: "))
print("|{0:<7}|{1:<10}|{2:<9}|".format("index", x1, x2))
print("_" * 30)
for i in range(n):
x1 = 3.9 * x1 * (1 - x1)
x2 = 3.9 * x2 * (1 - x2)
print("|{0:<7}|{1:<10.6f}|{2:<9.6f}|".format(i + 1, x1, x2))
main()
12题答案
def main():
principal = eval(input("Enter the initial principal: "))
apr = float(input("Enter the annual interest rate: "))
years = int(input("Enter the number of years: "))
print()
print("Year Value")
print("-------------------")
for i in range(years + 1):
print("{0:<8}${1:.2f}".format(i, principal))
principal = principal * (1 + apr)
main()
13题,略
14题答案
def main():
filename = input("Enter the filename: ")
infile = open(filename, 'r')
chars = infile.read()
words = chars.split()
lines = chars.split("\n")
infile.close()
print("Lines:", len(lines))
print("Words:", len(words))
print("Characters:", len(chars))
main()
15题答案
def mark(window, name, point, pos, m):
a = 2
Name = Text(Point(2, m+a - pos), name)
Mark = Rectangle(Point(5, m +0.45 +a- pos), Point(5 + point / 10, m-0.45+a - pos))
Mark.setWidth(2)
Mark.setFill(color_rgb(144,144,255))
Text(Point(18, m +a- pos), str(point)).draw(window)
Name.draw(window)
Mark.draw(window)
def main():
filename = input("Enter the name of the file: ")
infile = open(filename, "r", encoding="utf-8")
data = infile.read()
data1 = data.split("\n")
m = int(data1[0])
win = GraphWin("grades", 55*(m+2), 55*(m+2))
win.setCoords(0, 0, 20, m+3)
win.setBackground("white")
for i in range(1,len(data1)):
data2 = data1[i].split()
name,point = data2[0],float(data2[1])
mark(win, name, point, i, m)
print("Click to quit.")
infile.close()
win.getMouse()
win.close()
main()
16题答案
def main():
filename = input("Enter the name of the file:")
infile = open(filename, "r")
inf = infile.read()
win = GraphWin("Students' Grade", 300, 200)
win.setCoords(0, 0, 18, 12)
win.setBackground(color_rgb(10, 155, 20))
for i in range(11):
p = str(i)
n = inf.count(p)
drb(win, p, n, i)
win.getMouse()
win.close()
infile.close()
def drb(window, p, n, i):
u = i*1.58
Text(Point(u + 1,0.5),p).draw(window)
Text(Point(u + 1,1.5 + n),str(n)).draw(window)
bar = Rectangle(Point(u + 0.54,1),Point(u + 1.46,1 + n))
bar.setFill(color_rgb(34, 68, 255))
bar.setOutline("red")
bar.draw(window)
main()