选择结构:
if XXXXX:
XXXXX
else:
XXXXX
循环结构:
for i in range(1,10):(让i从1到9做循环,range()有三个参数,起始,终止,步长(可省略不写))但冒号不能省
XXXXXXXXXXXXXX
while 结束条件:
循环体
定义函数:
def 函数名(参数列表):
函数主体部分
if i in (1,3,5,7,810,12):
XXXXXXXXXXX 比较特殊,Python还可以这样玩
调用函数和C一样
global 用来声明全局变量
缺省函数:还是有点模糊
汉诺塔问题:Python求解
t = int(input('how many disks you want to move ?')) def hanoi(n,A,B,C): if n==1: print('disk:',n,'from',A,'to',C) else: hanoi(n-1,A,C,B) print('disk:', n, 'from', A, 'to', C) hanoi(n-1,B,A,C) print(hanoi(t,'A','B','C'))
停车问题:
import random def parking(low,high): if high-low<1: return 0 else: x=random.uniform(0,high-1) return parking(low,x)+1+parking(x+1,high) t=int(input('please input the distance:')) print(parking(0,5))