Python语言程序设计基础(第2版) 课后题 第五章

#e7.2DrawSevenSegDisplay.py
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))
def main():
    turtle.setup(800, 350, 200, 200)
    turtle.penup()
    turtle.fd(-350)
    turtle.pensize(5)
    drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
    turtle.hideturtle()
main()    





def factorial(num):
    if num == 0:
        return 1
    else:
        return num * factorial(num-1)
n = eval(input("请输入一个整数: "))
print(factorial(abs(int(n))))

请输入一个整数: 4
24
def reverse(s):
    if s == "":
        return s
    else:
        return reverse(s[1:]) + s[0]
str = input("请输入一个字符串: ")
print(reverse(str))


请输入一个字符串: agagafg
gfagaga
#e8.2DrawKoch.py
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(600,600)
    turtle.speed(0)
    turtle.penup()
    turtle.goto(-200, 100)
    turtle.pendown()
    turtle.pensize(2)
    level = 5
    koch(400,level) 
    turtle.right(120)
    koch(400,level)
    turtle.right(120)
    koch(400,level)
    turtle.hideturtle()
main()

#5.5
def isPrime(num):
    import math
    try:
        if type(num) == type(0.):
            raise TypeError
        r = int(math.floor(math.sqrt(num)))
    except TypeError:
        print('请输入整数')
        return None
    if num == 1:
        return False
    for i in range(2, r+1):
        if num % i == 0:
            return False
    return True

print(isPrime(2))
print(isPrime(3))
print(isPrime(4))
True
True
False
#5.6
from datetime import datetime

birthday = datetime(1999,3,23,15,15)
print(birthday)
print(birthday.isoformat())
print(birthday.isoweekday())
print(birthday.strftime("%Y-%%m-%d %H:%M"))
print('%s 年%s 月%s 日'%(birthday.year,birthday.month,birthday.day))
print('{0:%Y}-{0:%m}-{0:%d} {0:%a}'.format(birthday))
print('{0:%b}.{0:%d} {0:%Y}'.format(birthday))
print('{0:%d}{1:} {0:%b} {0:%Y}'.format(birthday, ['st','nd','rd','th'][birthday.day%10-1 if birthday.day%10<=3 
                                                                       else 3]))
1999-03-23 15:15:00
1999-03-23T15:15:00
2
1999-%m-23 15:15
1999 年3 月23 日
1999-03-23 Tue
Mar.23 1999
23rd Mar 1999
#5.7
def move(n, a, b, c):
    if(n == 1):
        print(a,"->",c)
        return
    move(n-1, a, c, b)
    move(1, a, b, c)
    move(n-1, b, a, c)
move(3, "a", "b", "c")

a -> c
a -> b
c -> b
a -> c
b -> a
b -> c
a -> c
上一篇:rabbitmq学习(一):AMQP协议,AMQP与rabbitmq的关系


下一篇:LightOJ 1104 Birthday Paradox【概率】