python语言程序设计基础课后答案-第五章(嵩天)

时间:2024-03-06 07:55:28

教材:

  1.七段数码管绘制

  2.函数的递归

  3.科赫曲线绘制

习题:

  1. 输出田字格。
  2. 实现isOdd()函数。
  3. 实现isNum()函数。
  4. 实现multi()函数。
  5. 实现isPrime()函数。
  6. 使用datetime库,对自己的生日输出不少于10种日期格式。
  7. 输入汉诺塔层数,输出整个移动流程。

 1.七段数码管绘制

import turtle, datetime
def drawLine(draw):   #绘制单段数码管
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    turtle.right(90)
def drawDigit(d):
    drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,6,8] else drawLine(False)
    turtle.left(90)
    drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
    drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20) 
def drawDate(date):  #获得要输出的数字
    for i in date:
        drawDigit(eval(i))  #注意: 通过eval()函数将数字变为整数
def main():
    turtle.setup(800, 350, 200, 200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(datetime.datetime.now().strftime(\'%Y%m%d\'))
    turtle.hideturtle()
main()

import turtle, datetime
def drawGap():#绘制数码管间隔
    turtle.penup()
    turtle.fd(5)
def drawLine(draw):   #绘制单段数码管
    drawGap()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    drawGap()
    turtle.right(90)
def drawDigit(d):
    drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,6,8] else drawLine(False)
    turtle.left(90)
    drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
    drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
    drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20) 
def drawDate(date):  #获得要输出的数字
    turtle.pencolor("red")
    for i in date:
        if i==\'-\':
            turtle.write(\'\',font=("Arial",18,"normal"))
            turtle.pencolor("green")
            turtle.fd(40)
        elif i==\'=\':
            turtle.write(\'\',font=("Arial",18,"normal"))
            turtle.pencolor("blue")
            turtle.fd(40)
        elif i==\'+\':
            turtle.write(\'\',font=("Arial",18,"normal"))
        else:
            drawDigit(eval(i))  #注意: 通过eval()函数将数字变为整数
def main():
    turtle.setup(800, 350, 200, 200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(datetime.datetime.now().strftime(\'%Y-%m=%d+\'))
    turtle.hideturtle()
main()

 (数码管绘制10进制变为16进制,作为字符串输入)

import turtle, datetime
a=input(\'\') 
def drawGap():#绘制数码管间隔
    turtle.penup()
    turtle.fd(5)
def drawLine(draw):   #绘制单段数码管
    drawGap()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    drawGap()
    turtle.right(90)
def drawDigit(d):
    drawLine(True) if d in [\'2\',\'3\',\'4\',\'5\',\'6\',\'8\',\'9\',\'a\',\'b\',\'d\',\'e\',\'f\'] else drawLine(False)
    drawLine(True) if d in [\'0\',\'1\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\',\'a\',\'b\',\'d\'] else drawLine(False)
    drawLine(True) if d in [\'0\',\'2\',\'3\',\'5\',\'6\',\'8\',\'9\',\'b\',\'c\',\'d\',\'e\'] else drawLine(False)
    drawLine(True) if d in [\'0\',\'2\',\'6\',\'8\',\'a\',\'b\',\'c\',\'d\',\'e\',\'f\'] else drawLine(False)
    turtle.left(90)
    drawLine(True) if d in [\'0\',\'4\',\'5\',\'6\',\'8\',\'9\',\'a\',\'b\',\'c\',\'e\',\'f\'] else drawLine(False)
    drawLine(True) if d in [\'0\',\'2\',\'3\',\'5\',\'6\',\'7\',\'8\',\'9\',\'a\',\'c\',\'e\',\'f\'] else drawLine(False)
    drawLine(True) if d in [\'0\',\'1\',\'2\',\'3\',\'4\',\'7\',\'8\',\'9\',\'a\',\'d\'] else drawLine(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20) 
def drawDate(date):  #获得要输出的数字
    turtle.pencolor("red")
    for i in date:
        drawDigit(i)
def drawDate1(date):  #获得要输出的数字
    turtle.pencolor("red")
    count=0
    for i in date:
        count+=1
        if count>2:
            drawDigit(i)
def main():
    turtle.setup(800, 350, 200, 200)
    turtle.penup()
    turtle.fd(-300)
    turtle.pensize(5)
    drawDate(a)
    turtle.fd(20)
    turtle.pendown()
    turtle.fd(145)
    turtle.penup()
    turtle.fd(20)
    b=hex(int(a))
    print(b)
    drawDate1(str(b))
    turtle.hideturtle()
main()

 

 

 2.科赫曲线绘制

import turtle
def koch(size,n):
    if n==0:
        turtle.fd(size)
    else:
        for angle in [0,60,-120,60]:
            turtle.left(angle)
            koch(size/3,n-1)
def main():
    turtle.setup(800,400)
    turtle.speed(0)#控制绘制速度
    turtle.penup()
    turtle.goto(-300,-50)
    turtle.pendown()
    turtle.pensize(2)
    koch(600,3)#0阶科赫曲线长度,阶数
    turtle.hideturtle()
main()

 

import turtle
def koch(size,n):
    if n==0:
        turtle.fd(size)
    else:
        for angle in [0,60,-120,60]:
            turtle.left(angle)
            koch(size/3,n-1)
def main():
    turtle.setup(400,400)
    turtle.speed(0)#控制绘制速度
    turtle.penup()
    turtle.goto(-200,-100)
    turtle.pendown()
    turtle.pensize(2)
    turtle.pencolor("green")
    level=5
    koch(300,4)
    turtle.right(120)
    turtle.pencolor("blue")
    koch(300,4)
    turtle.right(120)
    turtle.pencolor("red")
    koch(300,4)
    turtle.hideturtle()
main()

 

输出田字格。

def drawss(n):
    line=3*n+1
    for i in range(1,line+1):
        if i%3==1:
            print(n*"+----",end="")
            print("+")
        else:
            print("|    "*n,end="")
            print("|")
def main():
    n=eval(input("请输入您要的阶数:"))
    drawss(n)
main()

 

实现isOdd()函数。

def odd(d):
    if d%2==0:
        return True
    else :
        return False
d=int(input())
print(odd(d))

 

 

 

实现isNum()函数。

实现multi()函数。

def multi(*args):
    sum=1
    count=1
    for i in args:
        if type(i) is type(1) or type(i) is type(1.):
            sum*=i
    return sum
print(multi(2,3,1,0.5,4.99))
print(multi(2,1,\'str\'))

 

 

实现isPrime()函数。

 

使用datetime库,对自己的生日输出不少于10种日期格式。

输入汉诺塔层数,输出整个移动流程。