A: for loop
Nested loop of 1.1 for 99 print multiplication table
count = 0
for i in range(1, 10):
for a in range(1, i + 1):
count = i * a
print('{x} * {z} = {c} '.format(z=i, x=a, c=count), end='\t')
print()
Nesting of 1.2 for loop print pyramid
Tip follows
''' #max_level=5 * #current_level=1,空格数=4,*号数=1 *** #current_level=2,空格数=3,*号数=3 ***** #current_level=3,空格数=2,*号数=5 ******* #current_level=4,空格数=1,*号数=7 ********* #current_level=5,空格数=0,*号数=9 #数学表达式 空格数=max_level-current_level *号数=2*current_level-1 '''
# 方案一
y = '*'
for i in range(5):
x = (i * 2 + 1) * y
print(x.center(10, ' '))
# 方案二
y = '*'
for i in range(5):
print('{0: ^10}'.format((i * 2 + 1) * y))
For + 1.3 with rewrite the code range today morning dictation as tomorrow dictation content
username = 'egon'
password = '123'
for i in range(3):
user_name = input('your name:>')
user_pwd = input('your password:>')
if user_name == username and user_pwd == password:
print('登陆成功')
while True:
cmd = input('请输入您的命令>:')
if cmd == 'q':
break
else:
print('命令{x}正在运行'.format(x=cmd))
break
else:
print('账号密码错误请重试')
else:
print('三次登陆失败,自动退出')
II: String operations
Write the code, the following variables, functions as required to implement each (a total of 6 points, 0.5 points each Meixiao Ti)
name = " aleX"
1) corresponding to the variable name is removed on both sides of the space values, and outputs the processing result
res1 = name.strip()
print(res1)
2) determination whether the value corresponding to the variable name beginning with "al", and outputs the result
res2 = name.startswith('al')
print(res2)
3) corresponding to the variable name is determined whether the value of an "X" at the end, and outputs the result
res3 = name.endswith('X')
print(res3)
4) the value corresponding to the variable name in the "l" is replaced with "p", and outputs the result
res4 = name.replace('l','p')
print(res4)
5) The name of the corresponding variable value in accordance with "l" division, and outputs the result.
res5 = name.split('l')
print(res5)
6) The name becomes a variable value corresponding to upper case, and outputs the result
res6 = name.upper()
print(res6)
7) The name corresponding to the variable value becomes lower case, and the output
res7 = name.lower()
print(res7)
8) Please outputs of the two character values corresponding to the variable name?
res8 = name[1]
print(res8)
9) Please output the first three characters of the name value of the corresponding variable?
res9 = name[0:3]
print(res9)
10) Please post the output value of two variables corresponding character name?
res10 = name[-2:]
print(res10)
11) Request output value corresponding to the variable name index where "e" position?
res11 = name.find('e')
print(res11)
12) acquisition sequences, removing the last character. Such as: oldboy then get oldbo.
res12 = name [:-1]
print(res12)