python3 100例 一码人学习笔记(31-40)

题目31:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。

weeklist = {'M': 'Monday','T': {'u': 'Tuesday','h':'Thursday'}, 'W': 'Wednesday', 'F':'Friday','S':{'a':'Saturday','u':'Sunday'}}
sLetter1 = input("请输入首字母:")
sLetter1 = sLetter1.upper()

if (sLetter1 in ['T','S']):
    sLetter2 = input("请输入第二个字母:")
    print(weeklist[sLetter1][sLetter2])
else:
    print(weeklist[sLetter1])
请输入首字母:t
请输入第二个字母:u
Tuesday
Press any key to continue . . .

题目32:按相反的顺序输出列表的值。

a = ['one', 'two', 'three']
for i in a[::-1]:
    print (i)
three
two
one
Press any key to continue . . .

 题目33:按逗号分隔列表。

L = [1,2,3,4,5]
s1 = ','.join(str(n) for n in L)
print (s1)
1,2,3,4,5
Press any key to continue . . .

 题目34:练习函数调用。

def hello_world():
    print( 'hello world')
 
def three_hellos():
    for i in range(3):
        hello_world()
if __name__ == '__main__':
    three_hellos()
hello world
hello world
hello world
Press any key to continue . . .

 题目35:文本颜色设置。

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
print (bcolors.WARNING + "警告的颜色字体?" + bcolors.ENDC)

 题目36:求100之内的素数。

for i in range(2,100):
    if 0  not in [i%n for n in range(2,i)]:
        print( i)
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

题目37:对10个数进行排序。

list = []
for i in range(5):
    list.append( int(input('enter num{} number:'.format(i))))
list.sort()
print(list)
enter num0 number:16
enter num1 number:14
enter num2 number:13
enter num3 number:1
enter num4 number:2
[1, 2, 13, 14, 16]
Press any key to continue . . .

题目38:求一个3*3矩阵主对角线元素之和。

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum_ = 0
for i in range(0, 3):
    sum_ += matrix[i][i]
print (sum_)
15
Press any key to continue . . .

题目39:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

x=[1,3,5,6,88,99]
 

x=[1,3,5,6,88,99]
y=int(input("输入数字: "))
x.append(y)
x.sort()
print(x)
输入数字: 1
[1, 1, 3, 5, 6, 88, 99]
Press any key to continue . . .

题目40:将一个数组逆序输出。

if __name__ == '__main__':
    a = [9,6,5,4,1]
    print (a[::-1])
[1, 4, 5, 6, 9]
Press any key to continue . . .

猜你喜欢

转载自blog.csdn.net/qq_41905045/article/details/81167305