小言_互联网的博客

python代码小例子

438人阅读  评论(0)

用户登录检测

知识点:EOF异常,多行输入末尾要判断文件结束EOF,用try except来处理。

def main():
    list = []
    listTure = ['Kate', '666666']
    i = 1
    while i <= 3:
        try:
            for j in range(2):
                list.append(input())
                if listTure == list:
                    print("登录成功!")
                    break
                elif i == 3 and j==1:
                    print("3次用户名或者密码均有误!退出程序。 ")
            list.clear()
            i = i + 1
        except:
            break
main()

水仙花数

知识点:地板除法//,带小数的除法/、取余%。地板除法才是整除。

list = []
def WaterNum(n):
    sum =pow(n%10,3)+pow((n%100-n%10)//10,3)+pow(n//100,3)
    # print(sum)
    if sum==n:
        list.append(n)
    return list

def main():

    for i in range(100,1000):
        WaterNum(i)
    print(",".join(str(i) for i in list))
main()

三次方格式化

string = int(input(""))
print(str(pow(string,3)).center(20,'-'))

星号三角形

知识点:center()函数,第一个参数是一行中间显示的,第二个参数是填充字符。

num =int(input(''))
for i in range(1,num+1):
    if i%2==1:
        print(str('*'*i).center(num,' '))
    else:
        continue

重叠多边形

知识点:turtle库的使用

t.setup(650,350,200,200 )
t.penup()
t.fd(-250)
t.pendown()
t.color('black')
t.pensize(5)

for i in range(5):
    t.fd(40)
    t.seth(72*(1+i))

t.penup()
t.fd(20)
t.seth(-90)
t.fd(10)
t.pendown()
t.seth(36)
for i in range(5):

    t.fd(40)
    t.seth(36+72 * (1 + i))

t.done()

温度转换

知识点:format()的用法, print(‘F{:.2f}’.format(F)),输入:F87.65,输出:C30.92。format()作用是输出格式化。

def tempereture(str1):
    if str1[0]=='f' or str1[0]=='F':
        C = (float(str1[1:]) - 32) / 1.8
        print('C{:.2f}'.format(C))
    else:
        F = float(str1[1:]) * 1.8 + 32
        print('F{:.2f}'.format(F))
str2 =input()
tempereture(str2)

科赫雪花

知识点:递归用法,基例和链条两大特性。

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.penup()
    turtle.goto(-200,100)
    turtle.pendown()
    turtle.pensize(2)
    turtle.color('purple')
    level=4
    koch(400,level)
    turtle.right(120)
    koch(400,level)
    turtle.right(120)
    koch(400,level)
    turtle.hideturtle()
main()

画出当前计算机时间

import turtle,time

def drawGap():
    turtle.penup()
    turtle.fd(5)

def drawLine(draw):
    drawGap()
    turtle.pendown() if draw else turtle.penup()
    # drawcolor(digit)
    turtle.fd(40)
    drawGap()
    turtle.right(90)

def drawcolor(color):
    pencolor = ['red','green','blue','pink','black','purple','brown','beige','orange','yellow']
    turtle.color(pencolor[color%10])

def drawDigit(digit):
    drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False)
    drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False)
    drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)
    drawLine(True) if digit in [0,2,6,8] else drawLine(False)
    turtle.left(90)
    drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)
    drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False)
    drawLine(True) if  digit in [0,1,2,3,4,7,8,9] else drawLine(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)

def drawDate(data):
    turtle.color('red')
    for i in data:
        if i == '-':
            turtle.write("年",font=("Arial",18,"normal"))
            turtle.pencolor("green")
            turtle.fd(40)
        elif i == '=':
            turtle.write("月",font=("Arial",18,"normal"))
            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(-300)
    turtle.pensize(5)
    # turtle.color('red')
    drawDate(time.strftime("%Y-%m=%d+",time.gmtime()))
    # drawDate('19960307')
    turtle.hideturtle()
    turtle.done()
main()

转载:https://blog.csdn.net/erdaidai/article/details/106480144
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场