Selected Python Level 2 Exam

1: The Unicode encoding range of Chinese characters (0x4e00~0x9fa5)
receives the data input by the user. The data is only composed of letters and Chinese characters, and there are no other types of characters. Count and output the number of Chinese characters. The example is as follows:
Input:
wor1d world peace
output:
4

s = input("请输入中文和字母的组合: ")
____________
for c in s:
    if ____________:
        count += 1
print(count)
s = input("请输入中文和字母的组合: ")
count=0   #求和初始值
for c in s:
    if u'\u4e00' <= c <= u'\u9fa5':
        count += 1
print(count)

2: Investigate the usage of split
Fill in the code at the horizontal line to complete the following functions. Receive a comma-separated set of data entered by the user, where each data is an integer or floating-point number, and print out the maximum value in this set of data. Example:
Input:
1,3,5,7,9,7,5,3,1
Output:
9

s = input("请输入一组数据: ")
ls = ____________
lt = []
for i in ls:
    lt.append(____________)
print(max(lt))
s = input("请输入一组数据: ")
ls = s.split(',')
lt = []
for i in ls:
        lt.append(eval(i))
print(max(lt))


Three: format usage (<, >, ^, respectively left and right aligned, centered)
receives a floating-point number input by the user, and outputs the sum of the characters in the fractional part of the floating-point number, with 10 as the width, displayed on the right, using Asterisk * padding. For example:
Input:
1234.5678
Output:
********26

s = input("请输入一个小数: ")
s = s[::-1]
____________
for c in s:
    if c == '.':
        ____________
    cs += eval(c)
print('{____________}'.format(cs))
s = input("请输入一个小数: ")
s = s[::-1]
cs=0
for c in s:
    if c == '.':
        break
    cs += eval(c)
print('{:*>10}'.format(cs))

4: Investigate slice
The time library is a standard library related to time processing in the Python language. The ctime() function in the time library can convert a floating-point number representing time into a time format that humans can understand. The example is as follows:
import time
print(time .ctime(1519181231.0))
The output result is: Wed Feb 21 10:47:11 2018
Please get the user input time, extract and output the hour information. Taking the above time as an example, 10 should be output.

import time
t = input("请输入一个浮点数时间信息: ")
s = time.ctime(____________)
ls = s.split()
print(____________)
import time
t = input("请输入一个浮点数时间信息: ")
s = time.ctime(float(t))
ls = s.split()
print(ls[3][0:2])

5: Investigate random, ord()
has a file PY103.py in the candidate's folder, fill in the code at the horizontal line, and complete the following functions. Based on 26 lowercase letters and o~9 numbers, and the number input by the user as the seed, 10 8-digit passwords are randomly generated, and each password is printed out on a separate line. For example:
input:
125
output:

pot1wjta
ej460gqs
k515jdr8
1b1kedlf
y37c4mhx
1oa18pv5
pz6r37t7
xegd1q13
12wOksh6
pxuybhp9

import random

s = input("请输入随机种子: ")
ls = []
for i in range(26):
    ls.append(chr(ord('a')+i))
for i in range(10):
    ls.append(chr(____________))
    
random.seed(____________)
for i in range(10):
    for j in range(8):
        print(____________,end='')
    print()
import random

s = input("请输入随机种子: ")
ls = []
for i in range(26):
    ls.append(chr(ord('a')+i))
for i in range(10):
    ls.append(chr(ord('0')+i))
    
random.seed(eval(s))
for i in range(10):
    for j in range(8):
        print(random.choice(ls),end='')
    print()

Guess you like

Origin blog.csdn.net/weixin_55159605/article/details/123339622