3.1 if/while/for
3.2 解决数学难题
3.3 Python实例
3.1 if/while/for
'''
#
if 判断条件:
执行语句
elif 判断条件:
执行语句
else:
执行语句
#
while 判断条件:
执行语句
#
break: 跳出循环
#
continue: 跳出当次循环
#
for item in sequence:
执行语句
'''
# lsit 遍历
l = ['a', 'b', 'c', 'd', 'e']
for x, y in enumerate(l):
print(x, y)
3.2 解决数学难题
# ABCD * 9 = DBCA 答案:A=1 B=0 C=8 D=9
for A in range(1, 10):
for B in range(0, 10):
for C in range(0, 10):
for D in range(1, 10):
start = 1000 * A + 100 * B + 10 * C + D
end = 1000 * D + 100 * C + 10 * B + A
if start * 9 == end:
print("A={0};B={1};C={2};D={3}".format(A, B, C, D))
print("{0} * 9 = {1}".format(start, end))
3.3 Python实例
# 阶乘相加
def one(n):
total = 1
if n == 0:
total = 1
else:
for i in range(1, n+1):
total *= i
return total
result = 0
n = int(input("输入<=0的数字:"))
for i in range(0, n+1):
result += one(i)
print(result)