第一章 过程型程序设计快速入门 --习题
只是做了一部分,没有全部做,有的题目,2题合并为一题了。
1.1
View Code
1 numbers = []
2 count = 0
3 sum = 0
4 mean = 0
5
6 while True:
7 try:
8 line = input("enter a number or Enter to finish:")
9 if line:
10 num = int(line)
11 if num not in numbers:
12 numbers.append(num)
13 count += 1
14 sum += num
15 else:
16 break
17 except ValueError as err:
18 print(err)
19
20 print("numbers:",numbers)
21
22 for i in range(0,len(numbers)-1,1):
23 for j in range(i + 1,len(numbers),1):
24 if numbers[i] > numbers[j]:
25 numbers[i],numbers[j] = numbers[j],numbers[i]
26
27 print("sort numbers:",numbers)
28 mean = sum/count
29 print("count =",count,"sum =",sum,"lowest =",numbers[0],"highest =",numbers[-1],"mean =",mean)
30
31 middle = 0
32 l = len(numbers)
33 if l%2 == 1:
34 middle = numbers[l // 2]
35 else:
36 middle = (numbers[l // 2] + numbers[l // 2 - 1])/2
37
38 print("middle =",middle)
1.2
View Code
1 import sys
2
3 Zero = ["*****","* *","* *","* *","*****"]
4 One = [" *"," *"," *"," *"," *"]
5 Two = ["*****"," *","*****","* ","*****"]
6 Three = ["*****"," *","*****"," *","*****"]
7 Four = ["* *","* *","*****"," *"," *"]
8 Five = ["*****","* ","*****"," *","*****"]
9 Six = ["*****","* ","*****","* *","*****"]
10 Seven = ["*****"," *"," *"," *"," *"]
11 Eight = ["*****","* *","*****","* *","*****"]
12 Nine = ["*****","* *","*****"," *","*****"]
13
14 Digits = [Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine]
15
16 try:
17 digits = sys.argv[1]
18 row = 0
19 while row < 6:
20 line = ""
21 column = 0
22 while column < len(digits):
23 number = int(digits[column])
24 digit = Digits[number]
25 num_line = ""
26 for c in digit[row]:
27 if c == "*":
28 num_line += str(number)
29 else:
30 num_line += " "
31
32 line += num_line + " "
33 column += 1
34 print(line)
35 row += 1
36 except IndexError:
37 print("usage:bigdigits.py <number>")
38 except ValueError as err:
39 print(err,"in",digits)