嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av15123607/?from=search&seid=10211084839195730432#page=25 中的35-37讲

#coding=gbk
#嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av15123607/?from=search&seid=10211084839195730432#page=25 中的35-37讲
# 处理多个银行账户的程序
"""
def addInterest(balances,rate):
  for i in range(len(balances)):
    balances[i] = balances[i] * (1 + rate)

def test():
  amounts = [1000, 105, 3500, 739]
  print(id(amounts)) #实参amounts内存地址
  rate = 0.05
  addInterest(amounts,rate) #实参amounts在调用函数时发生了改变
  print(id(amounts)) #实参amounts内存地址没有发生改变
  print(amounts)
  amounts.append(700)
  print(id(amounts)) #实参amounts内存地址没有发生改变
  print(amounts)

test()
"""
#程序模块化例子
#程序1:
"""
print("This program plots the growth of a 10-year investment.")
principal = eval(input("Enter the initial principal: "))
apr = eval(input("Enter the annualized interest rate: "))
for year in range(1,11):
  principal = principal * (1 + apr)
  print("%2d" % year, end='')
  total = int(principal*4/1000)
  print("*" * total)
print("0.0K 2.5K 5.0K 7.5K 10.0K")
"""

#对程序1进行模块化
"""
def creatTable(principal, apr):
  for year in range(1,11):
    principal = principal * (1 + apr)
    print("%2d" % year, end='')
    total = caculateNum(principal)
    print("*" * total)
  print("0.0K 2.5K 5.0K 7.5K 10.0K")

def caculateNum(principal):
  total = int(principal*4/1000)
  return total

def main():
  print("This program plots the growth of a 10-year investment.")
  principal = eval(input("Enter the initial principal: "))
  apr = eval(input("Enter the annualized interest rate: "))
  creatTable(principal,apr)

main()
"""
# python的参数是通过值还传递的
# 如果变量是可变对象,返回到调用程序后,该对象会呈现被修改后的状态。

# 递归定义的特征:
# 有一个或多个基例是不需要再次递归的
# 所有的递归链都要以一个基例结尾
# 递归每次调用都会引起新函数的开始
# 递归有本地值的副本,包括该值的参数
# 阶乘递归函数中,每次函数调用中的相关n值在中途的递归链暂时存储,并在函数返回时使用。

# 阶乘的递归定义函数:
"""
def fact(n):
  if n == 0:
    return 1
  else:
    return n * fact(n-1)
"""
# 字符串反转递归函数
def reverse(s):
  if s == "":
    return s
  else:
    return reverse(s[1:]) + s[0]
print(reverse("iloveyou"))
#
# turtle库的常用指令
# forward(distance)将箭头移到某个指定坐标
# left(angel) right(angel)
# penup()提起笔,用于另起一个地方绘制时用,与pendown()配对使用。
# goto(x,y)
# home()
# circle(radius)
# speed()
#
# 用turtle库绘制并填充一个五角星的程序
"""
from turtle import Turtle

p = Turtle()
p.speed(1)
p.pensize(5)
p.color("black","yellow")
p.begin_fill()
for i in range(5):
  p.forward(200)
  p.right(144)
p.end_fill()
"""
# 用递归函数画树:
from turtle import Turtle

def maketree(x,y):
  p = Turtle()
  p.speed(0.1)
  p.color("green")
  p.pensize(5)
  p.hideturtle()
  p.getscreen().tracer(30,0)
  p.left(90)
  p.penup()
  p.goto(x,y)
  p.pendown()
  t = tree([p],110,65,0.6375)
  print(len(p.getscreen().turtles())) #用了多少个turtle绘制

def tree(plist, l, a, f):
  if l > 5:
    lst = []
    for p in plist:
      p.forward(l)
      q = p.clone()
      p.left(a)
      q.right(a)
      lst.append(p)
      lst.append(q)
    tree(lst, l*f, a, f)

def main():
  maketree(-200, 200)
  maketree(0, 0)
  maketree(200, -200)

main()

posted on 2017-11-06 17:36  大蛇Python  阅读(775)  评论(0编辑  收藏  举报

导航