python练习03_20200719

1. 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

A = []
B = []
C = []
for i in range(100):
    num = random.randint(0, 100)
    if num >= 90:
        A.append(num)
    elif 60 <= num and num < 90:
        B.append(num)
    else:
        C.append(num)
print(A, B, C)
print(len(A), len(B), len(C))

python练习03_20200719_第1张图片
2. 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

input = raw_input()   #不能区分中文,汉字占三位
alpha = []
space = []
digit = []
other = []

for i in input:
    if i.isdigit():
        digit.append(i)
    elif i.isalnum():
        alpha.append(i)
    elif i.isspace():
        space.append(i)
    else:
        other.append(i)
print(alpha,space,digit,other)
print(len(alpha),len(space),len(digit),len(other))

python练习03_20200719_第2张图片

#统计中文
import re
str = raw_input()
str = str.decode('utf-8') #解码成 unicode,汉字占一位
word = 0
num = 0
space = 0
other = 0
for i in str:
    if re.match(r'\d', i):
        num += 1
    elif re.match(r'\w', i) and not re.match(r'\d', i):
        word += 1
    elif re.match(' ', i):
        space += 1
    else:
        other += 1
print(word, num, space, other)

3. 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

num = 100.0
high = []       #每次反弹高度
sum = 100.0    #路径长度
time = int(raw_input("次数:"))
for i in range(time):
    num /= 2
    high.append(num)
    if i != time - 1:
        sum += num * 2

print("每次反弹高度为%s米" %high)
print("共经过%f米" %sum)

python练习03_20200719_第3张图片
4. 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

B = ['x', 'y', 'z']
for a in B:
    for b in B:
        for c in B:
            if a != b and b != c and c != a:
                if a != B[0] and c == B[1]:
                    print('a和%s比赛,b和%s比赛,c和%s比赛' %(a, b, c))

python练习03_20200719_第4张图片
5. 题目:打印出如下图案(菱形):

   *
  ***
 *****
*******
 *****
  ***
   *
j = 4
for i in range(1, 8, 2):
    j -= 1
    print(" " * j + "*" * i)
k = 1
for i in range(1, 8, 2):
    print(" " * k + "*" * (6 - i))
    k += 1

python练习03_20200719_第5张图片

 


 

你可能感兴趣的:(自动化_python学习,python)