当前位置:首页>python>阿尔法python习题整理(全)

阿尔法python习题整理(全)

  • 2026-06-29 04:40:15
阿尔法python习题整理(全)

第二章 Python基本语法元素

温度转换

温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fahrenheit)。‬

编程实现:将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度。‬

转换算法如下:(C表示摄氏度、F表示华氏度)‬

C = ( F - 32 ) / 1.8F = C * 1.8 + 32
TempStr = input("请输入带有符号的温度值:")if TempStr[-1in ['F','f']:    C = (eval(TempStr[0:-1])-32)/1.8    print("{:.2f}C".format(C))    #print("C{:.2f}".format(C))elif TempStr[-1in ['C','c']:    F = 1.8*eval(TempStr[0:-1])+32    print("{:.2f}F".format(F))else:    print("输入格式错误")

注释所有行

使用注释,使得下面所有的代码不在控制台显示。

#print('我是第一行')#print('我是第二行')'''print('我是第三行')print('我是第四行')print('我是第五行')'''

BMI

# 1.创建表示身高的变量 height 并赋值 1.8height = 1.8# 2.创建表示体重的变量 weight 并赋值 81weight = 81# 3.创建表示BMI值的变量 bmi 并计算bmi = 81/(height*height)# 4.将 bmi 打印输出print(bmi)

汇率兑换程序

人民币和美元是世界上通用的两种货币之一,按照温度转换程序的设计思路,以1 美元 = 7 人民币的汇率编写一个美元和人民币的双向兑换程序。

要求如下:

(1) 输入输出的人民币采用大写的 RMB 开头,金额可以是整数或小数,如:RMB123 指人民币123元;

(2) 输入输出的美元采用大写的 USD 开头,金额可以是整数或小数,如:USD20 指美元 20 元;

(3) 输出保留小数点后两位,方法为:print("%.2f" % a)。输入格式错误时,输出提示:输入格式错误;

(4) 使用 input() 获得输入时,不要增加提示字符串。

TempStr=input("请输入带字符的金额")if TempStr[0:3in 'RMB':    #D = int(eval(TempStr[0:-1])/6)    a = float(TempStr[3:])    a = a/7    print("USD%.2f" % a)elif TempStr[0:3in 'USD':    #R = int(eval(TempStr[0:-1])*6)    r = float(TempStr[3:])    r = r*7    print("RMB%.2f" % r)else:    print("输入格式错误")

冰墩墩

import turtleturtle.title('BingDunDun(阿尔法编程)')turtle.speed(10)  # 速度#左手turtle.penup()turtle.goto(177112)turtle.pencolor("lightgray")turtle.pensize(3)turtle.fillcolor("white")turtle.begin_fill()turtle.pendown()turtle.setheading(80)turtle.circle(-45200)turtle.circle(-30023)turtle.end_fill()# 左手内turtle.penup()turtle.goto(18295)turtle.pencolor("black")turtle.pensize(1)turtle.fillcolor("black")turtle.begin_fill()turtle.setheading(95)turtle.pendown()turtle.circle(-37160)turtle.circle(-2050)turtle.circle(-20030)turtle.end_fill()# 轮廓# 头顶turtle.penup()turtle.goto(-73230)turtle.pencolor("lightgray")turtle.pensize(3)turtle.fillcolor("white")turtle.begin_fill()turtle.pendown()turtle.setheading(20)turtle.circle(-25035)# 左耳turtle.setheading(50)turtle.circle(-42180)# 左侧turtle.setheading(-50)turtle.circle(-19030)turtle.circle(-32045)# 左腿turtle.circle(12030)turtle.circle(20012)turtle.circle(-1885)turtle.circle(-18023)turtle.circle(-20110)turtle.circle(15115)turtle.circle(10012)# 右腿turtle.circle(15120)turtle.circle(-15110)turtle.circle(-15030)turtle.circle(-1570)turtle.circle(-15010)turtle.circle(20035)turtle.circle(-15020)# 右手turtle.setheading(-120)turtle.circle(5030)turtle.circle(-35200)turtle.circle(-30023)# 右侧turtle.setheading(86)turtle.circle(-30026)# 右耳turtle.setheading(122)turtle.circle(-53160)turtle.end_fill()# 右耳内turtle.penup()turtle.goto(-130180)turtle.pencolor("black")turtle.pensize(1)turtle.fillcolor("black")turtle.begin_fill()turtle.pendown()turtle.setheading(120)turtle.circle(-28160)turtle.setheading(210)turtle.circle(15020)turtle.end_fill()# 左耳内turtle.penup()turtle.goto(90230)turtle.setheading(40)turtle.begin_fill()turtle.pendown()turtle.circle(-30170)turtle.setheading(125)turtle.circle(15023)turtle.end_fill()# 右手内turtle.penup()turtle.goto(-180, -55)turtle.fillcolor("black")turtle.begin_fill()turtle.setheading(-120)turtle.pendown()turtle.circle(5030)turtle.circle(-27200)turtle.circle(-30020)turtle.setheading(-90)turtle.circle(30014)turtle.end_fill()# 左腿内turtle.penup()turtle.goto(108, -168)turtle.fillcolor("black")turtle.begin_fill()turtle.pendown()turtle.setheading(-115)turtle.circle(11015)turtle.circle(20010)turtle.circle(-1880)turtle.circle(-18013)turtle.circle(-2090)turtle.circle(1560)turtle.setheading(42)turtle.circle(-20029)turtle.end_fill()# 右腿内turtle.penup()turtle.goto(-38, -210)turtle.fillcolor("black")turtle.begin_fill()turtle.pendown()turtle.setheading(-155)turtle.circle(15100)turtle.circle(-10110)turtle.circle(-10030)turtle.circle(-1565)turtle.circle(-10010)turtle.circle(20015)turtle.setheading(-14)turtle.circle(-20027)turtle.end_fill()# 右眼# 眼圈turtle.penup()turtle.goto(-64120)turtle.begin_fill()turtle.pendown()turtle.setheading(40)turtle.circle(-35152)turtle.circle(-10050)turtle.circle(-35130)turtle.circle(-10050)turtle.end_fill()# 眼珠turtle.penup()turtle.goto(-4755)turtle.fillcolor("white")turtle.begin_fill()turtle.pendown()turtle.setheading(0)turtle.circle(25360)turtle.end_fill()turtle.penup()turtle.goto(-4562)turtle.pencolor("darkslategray")turtle.fillcolor("darkslategray")turtle.begin_fill()turtle.pendown()turtle.setheading(0)turtle.circle(19360)turtle.end_fill()turtle.penup()turtle.goto(-4568)turtle.fillcolor("black")turtle.begin_fill()turtle.pendown()turtle.setheading(0)turtle.circle(10360)turtle.end_fill()turtle.penup()turtle.goto(-4786)turtle.pencolor("white")turtle.fillcolor("white")turtle.begin_fill()turtle.pendown()turtle.setheading(0)turtle.circle(5360)turtle.end_fill()# 左眼# 眼圈turtle.penup()turtle.goto(5182)turtle.fillcolor("black")turtle.begin_fill()turtle.pendown()turtle.setheading(120)turtle.circle(-32152)turtle.circle(-10055)turtle.circle(-25120)turtle.circle(-12045)turtle.end_fill()# 眼珠turtle.penup()turtle.goto(7960)turtle.fillcolor("white")turtle.begin_fill()turtle.pendown()turtle.setheading(0)turtle.circle(24360)turtle.end_fill()turtle.penup()turtle.goto(7964)turtle.pencolor("darkslategray")turtle.fillcolor("darkslategray")turtle.begin_fill()turtle.pendown()turtle.setheading(0)turtle.circle(19360)turtle.end_fill()turtle.penup()turtle.goto(7970)turtle.fillcolor("black")turtle.begin_fill()turtle.pendown()turtle.setheading(0)turtle.circle(10360)turtle.end_fill()turtle.penup()turtle.goto(7988)turtle.pencolor("white")turtle.fillcolor("white")turtle.begin_fill()turtle.pendown()turtle.setheading(0)turtle.circle(5360)turtle.end_fill()# 鼻子turtle.penup()turtle.goto(3780)turtle.fillcolor("black")turtle.begin_fill()turtle.pendown()turtle.circle(-8130)turtle.circle(-22100)turtle.circle(-8130)turtle.end_fill()# 嘴turtle.penup()turtle.goto(-1548)turtle.setheading(-36)turtle.begin_fill()turtle.pendown()turtle.circle(6070)turtle.setheading(-132)turtle.circle(-45100)turtle.end_fill()# 彩虹圈turtle.penup()turtle.goto(-135120)turtle.pensize(5)turtle.pencolor("cyan")turtle.pendown()turtle.setheading(60)turtle.circle(-165150)turtle.circle(-13078)turtle.circle(-25030)turtle.circle(-138105)turtle.penup()turtle.goto(-131116)turtle.pencolor("slateblue")turtle.pendown()turtle.setheading(60)turtle.circle(-160144)turtle.circle(-12078)turtle.circle(-24230)turtle.circle(-135105)turtle.penup()turtle.goto(-127112)turtle.pencolor("orangered")turtle.pendown()turtle.setheading(60)turtle.circle(-155136)turtle.circle(-11686)turtle.circle(-22030)turtle.circle(-134103)turtle.penup()turtle.goto(-123108)turtle.pencolor("gold")turtle.pendown()turtle.setheading(60)turtle.circle(-150136)turtle.circle(-10486)turtle.circle(-22030)turtle.circle(-126102)turtle.penup()turtle.goto(-120104)turtle.pencolor("greenyellow")turtle.pendown()turtle.setheading(60)turtle.circle(-145136)turtle.circle(-9083)turtle.circle(-22030)turtle.circle(-120100)turtle.penup()# 爱心turtle.penup()turtle.goto(220115)turtle.pencolor("brown")turtle.pensize(1)turtle.fillcolor("brown")turtle.begin_fill()turtle.pendown()turtle.setheading(36)turtle.circle(-8180)turtle.circle(-6024)turtle.setheading(110)turtle.circle(-6024)turtle.circle(-8180)turtle.end_fill()# 五环turtle.penup()turtle.goto(-5, -170)turtle.pendown()turtle.pencolor("blue")turtle.circle(6)turtle.penup()turtle.goto(10, -170)turtle.pendown()turtle.pencolor("black")turtle.circle(6)turtle.penup()turtle.goto(25, -170)turtle.pendown()turtle.pencolor("brown")turtle.circle(6)turtle.penup()turtle.goto(2, -175)turtle.pendown()turtle.pencolor("lightgoldenrod")turtle.circle(6)turtle.penup()turtle.goto(16, -175)turtle.pendown()turtle.pencolor("green")turtle.circle(6)turtle.penup()turtle.pencolor("black")turtle.goto(-16, -160)turtle.write("BEIJING 2022", font=('Arial', 10, 'bold italic'))turtle.hideturtle()turtle.done()

求圆面积周长

输入圆的半径r,计算圆的面积(area)和周长(circumference)。

注意: 所有数保留两位小数。π 取值 3.14。方法为:print("%.2f" % a)

#请使用 input() 输入圆的半径 rr = float(input("请输入半径:"))#请计算圆的面积和周长,并将计算结果输出area=3.14*r*r;circumference=3.14*2*r;print("面积:%.2f" %area);print("周长:%.2f" %circumference);

同切圆绘制

import turtleturtle.pensize(2)turtle.circle(8)turtle.circle(32)turtle.circle(64)turtle.circle(100)

五角星绘制

from turtle import *fillcolor("red")begin_fill()while True:    forward(150)    right(144)    if distance(00) < 1:        breakend_fill()done()

温度转换

温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fahrenheit)。‬

编程实现:将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度。‬

TempStr = input("请输入带有符号的温度值:")if TempStr[-1in ['F','f']:      C=(eval(TempStr[0:-1])-32)/1.8      print("转换后的温度是%.2fC" %C)elif TempStr[-1in ['C','c']:      F=1.8*eval(TempStr[0:-1])+32      print("转换后的温度是{:.2f}F".format(F))else:      print("输入格式错误")

打印数据类型

num1 = 30num2 = 7num3 = num1/num2# 在下面将 num3 转换成 int 类型,使得最终输出是 <class 'int'>num3=int(num1/num2)print(type(num3))

交换变量值

num1 = int(input())num2 = int(input())# 请不要修改源代码最上面2行# 请在下面交换 num1, num2 两个变量的值num1,num2=num2,num1

计算器

# 请使用 input() 输入两个数 num1, num2num1=float(input())num2=float(input())# 请分别计算 num1,num2 的和(summation)、差(difference)、积(product)、商(quotient),并将计算结果输出summation=num1+num2difference=num1-num2product=num1*num2quotient=num1/num2print("和为:%.2f" %summation)print("差为:%.2f" %difference)print("积为:%.2f" %product)print("商为:%.2f" %quotient)

输入身高体重并计算BMI

# 请使用 input 获取键盘输入height=float(input('请输入年龄:'))# 请使用 input 获取键盘输入weight=float(input('请输入体重:'))bmi = weight / (height ** 2)print(bmi)

Hello World的条件输出

请用程序实现

获得用户输入的一个整数num,参考该整数值,打印输出"Hello World"。

要求如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

(1) 如果输入值是 0,直接输出"Hello World"‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

(2) 如果输入值大于 0,以两个字符一行方式输出"Hello  World"(空格也是字符)‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

(3) 如果输入值小于 0,以垂直方式输出"Hello World"

# 请使用 input() 输入一个整数 numnum=int(input())# 请对num进行判断if num==0:    print("Hello World")elif num>0:    print("He\nll\n\nWo\nrl\nd")else:    print("H\ne\nl\nl\no\n\nW\no\nr\nl\nd")

第三章 基本图形绘制

绘制玫瑰花

import turtleturtle.setup(350600)alpha = turtle.Turtle()alpha.penup()alpha.left(90)alpha.fd(200)alpha.pendown()alpha.right(90)alpha.fillcolor("red")alpha.begin_fill()alpha.circle(10180)alpha.circle(25110)alpha.left(50)alpha.circle(6045)alpha.circle(20170)alpha.right(24)alpha.fd(30)alpha.left(10)alpha.circle(30110)alpha.fd(20)alpha.left(40)alpha.circle(9070)alpha.circle(30150)alpha.right(30)alpha.fd(15)alpha.circle(8090)alpha.left(15)alpha.fd(45)alpha.right(165)alpha.fd(20)alpha.left(155)alpha.circle(15080)alpha.left(50)alpha.circle(15090)alpha.end_fill()alpha.left(150)alpha.circle(-9070)alpha.left(20)alpha.circle(75105)alpha.setheading(60)alpha.circle(8098)alpha.circle(-9040)alpha.left(180)alpha.circle(9040)alpha.circle(-8098)alpha.setheading(-83)alpha.fd(30)alpha.left(90)alpha.fd(25)alpha.left(45)alpha.fillcolor("green")alpha.begin_fill()alpha.circle(-8090)alpha.right(90)alpha.circle(-8090)alpha.end_fill()alpha.right(135)alpha.fd(60)alpha.left(180)alpha.fd(85)alpha.left(90)alpha.fd(80)alpha.right(90)alpha.right(45)alpha.fillcolor("green")alpha.begin_fill()alpha.circle(8090)alpha.left(90)alpha.circle(8090)alpha.end_fill()alpha.left(135)alpha.fd(60)alpha.left(180)alpha.fd(60)alpha.right(90)alpha.circle(20060)

蟒蛇绘制

import turtleturtle.setup(650,350,200,200)turtle.penup()turtle.fd(-250)turtle.pendown()turtle.pensize(20)turtle.pencolor("purple")turtle.seth(-40)for i in range(4):  turtle.circle(40,80)  turtle.circle(-40,80)turtle.circle(40,80/2)turtle.fd(40)turtle.circle(16,180)turtle.fd(40*2/3)turtle.done()

正方型绘制

import turtlealpha = turtle.Turtle()alpha.color('red')alpha.forward(100)alpha.left(90)# 完善代码,画一个正方形。alpha.forward(100)alpha.left(90)alpha.forward(100)alpha.left(90)alpha.forward(100)alpha.left(90)

八边形

import turtle as tt.pensize(2)for i in range(8):    t.fd(100)    t.left(45)

八角形

import turtle as tt.pensize(2)for i in range(8):    t.fd(150)    t.left(135)

叠边形

import turtle as tt.pensize(2)for i in range(9):    f.td(150)    t.left(80)

使用循环绘制五角星

import turtlealpha = turtle.Turtle()steps = 100angle1 = (180 * (5 - 2)) / 5angle2 = 180 - angle1angle = 2 * angle2for i in range(5):    alpha.fd(60)    alpha.right(angle)

风轮绘制

import turtle as tt.pensize(3)for i in range(4):    t.fd(150)    t.right(90)    t.circle(-150,45)    t.right(90)    t.fd(150)    t.right(45)t.done()    alpha.right(angle)

第四章 基本数据类型

从尾到头

编写程序实现,将数字倒序输出。

输入一个小于 500 的三位整数,将这个整数乘 2 得到一个新的数,将这个新的数从尾到头倒序输出。

# 从键盘输入一个三位数num = int(input("请输入一个小于500的三位整数"))# 将其乘 2 后倒序输出num1 = str(num*2)i = len(num1)while(i>0):    print(num1[i-1],end="")    i = i-1

抹零

现有小区停车收费规则如下,每小时收 1 元,1 小时内免费,不足一小时按 1 小时算。

# 请输入停车时长hour = float(input("请输入停车时间:"))# 请计算需要收取的停车费if hour<1:     print("收费0元")else:     print("收费{:.0f}元".format(int(hour)))    

实现加密器

编程实现:输入一个整数num,将输入的数字进行加密,并将加密后的结果输出。

以下为加密的规则:

  1. 加密结果 = (整数 * 10 + 5) / 2 + 3.14159
  2. 加密结果仍为一整数
# 请使用 input() 输入一个整数 numnum = int(input("请输入一个整数:"))# 请对数字 num 进行加密,并将加密结果输出print(int((num*10+5)/2+3.14159)

判断奇偶数

# 请使用 input() 输入一个整数 numnum=int(input("请输入一个整数"))# 请判断这个数是奇数还是偶数if num%2==0:   print("even")else:   print("odd")

公倍数

# 请使用 input() 输入一个正整数 numnum=int(input())# 请判断这个正整数是否是 5 和 7 的公倍数if num%5==0 and num%7==0:   print("yes")else:   print("no")

判断平闰年

# 请使用 input() 输入一个年份 yearyear=int(input())# 请判断这个年份是否为闰年if (year%4==0and (year%100!=0or (year%400==0):   print("leap year")else:   print("common year")

天天向上的力量第一问

计算千分之一的力量。

要求如下:

(1) 计算一年 365 天,每天进步千分之一,累计进步到多少,并将计算结果输出。

(2) 计算一年 365 天,每天退步千分之一,累计剩下多少,并将计算结果输出。

(3) 输出时,在一行内输出进步的结果和退步的结果。

解1

dayup=pow(1.001,365)daydown=pow(0.999,365)print("向上:{:.2f},向下:{:.2f}".format(dayup,daydown))

解2

dayup = 1daydown = 1daystep = 0.001for i in range(365):    dayup = dayup * (1 + daystep)for i in range(365):    daydown = daydown * (1 - daystep)print("向上:{:.2f}".format(dayup),end="")print("向下:{:.2f}".format(daydown),end="")

天天向上的力量第二问

计算千分之五和百分之一的力量。

要求如下:

(1) 计算一年 365 天,每天进步千分之五或百分之一,累计进步到多少,并将计算结果输出。

(2) 计算一年 365 天,每天退步千分之五或百分之一,累计剩下多少,并将计算结果输出。

(3) 输出时,在一行内输出进步的结果和退步的结果。

解1

dayfactor=0.005dayup=pow(1+dayfactor,365)daydown=pow(1-dayfactor,365)print("向上:{:.2f},向下:{:.2f}".format(dayup,daydown))

解2

dayup1 = 1dayup2 = 1daydown1 = 1daydown2 = 1daystep1 = 0.005daystep2 = 0.01for i in range(365):    dayup1 = dayup1 * (1 + daystep1)    dayup2 = dayup2 * (1 + daystep2)for i in range(365):    daydown1 = daydown1 * (1 - daystep1)    daydown2 = daydown2 * (1 - daystep2)print("向上:{:.2f}".format(dayup1),end=" ")print("{:.2f}".format(dayup2),end=" ")print("向下:{:.2f}".format(daydown1),end=" ")print("{:.2f}".format(daydown2),end="")

天天向上的力量第三问

工作日的力量。

要求如下:

(1) 一年 365 天,一周 5 个工作日,每天进步 1%;

(2) 一年 365 天,一周 2 个休息日,每天退步 1%;

(3) 计算按照工作日和休息日的进步或退步情况,经过一年 365 天后,累计剩下多少,并将计算结果输出。

(4) 输出时,在一行内输出计算的结果。

dayup = 1.0dayfactor = 0.01for i in range(365):    if i % 7 in [6, 0]:           dayup = dayup * (1 - dayfactor)    else:        dayup = dayup * (1 + dayfactor)print("工作日的力量:{:.2f}".format(dayup))        

天天向上的力量第四问

工作日模式要努力到什么水平,才能与每天努力进步 1% 一样?‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

  • A君: 一年 365 天,每天进步 1%,不停歇,365天之后,达到37.78  ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬
  • B君: 一年 365 天,每周工作 5 天休息 2 天,休息日每天退步 1%,那么工作日应该多努力才能追上A君呢?

请用程序实现

计算要使 B君 和 A君 一年后的进步程度一样,那么 B君 每天需要进步的值,并将计算结果输出。

要求如下:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

(1) ‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬一年 365 天,以第 0 天的能力值为基数,记为  1.0;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

(2) ‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬当好好学习时,能力值相比前一天提高 1%;当没有学习时,由于遗忘等原因能力值相比前一天下降 1%;

(3) 输出保留小数点后三位,冒号后有一个空格。如:工作日的努力参数是: 0.019

def dayUP(df):    dayup=1    for i in range(365):        if i%7 in [6,0]:            dayup = dayup*(1-0.01)        else:            dayup = dayup*(1+df)    return dayupdayfactor = 0.01while dayUP(dayfactor)<37.78:    dayfactor += 0.001print("工作日的努力参数是:{:.3f}".format(dayfactor))

删除字符

删除预置代码中字符串string中的一部分。

要求如下:

(1) 分两行输入两个整数,第一个整数表示字符串 string 的索引 begin,第二个整数表示需要删除的长度 length。

(2) 将字符串 string 中,从索引 begin 开始,长为 length 的子字符串删除。

(3) 输出字符串 string 删除后剩下的部分。

string = 'abcdefghijklmnopqrstuvwxyz'# 请使用 input() 输入索引 begin 和长度 lengthbegin = int(input("请输入索引开始值:"))length = int(input("请输入长度:"))# 请将字符串 string 中,从索引 begin 开始,长为 length 的字符串删除,并将剩下的字符串内容输出remain=string[:begin]+string[begin+length:]print(remain)

插入字符

已知字符串 s ,请将另一个字符串 sub 添加到字符串 s 的指定索引位置中,并将添加后的 s 输出。

s = 'abcdefghijklmnopqrstuvwxyz'sub = input("请输入要添加的字符串")pos = int(input())str1 = list(s)str1.insert(pos,sub)for i in range(len(str1)):    print(str1[i],end="")

你中无我

给定两个字符串 s1, s2,删除字符串 s1 中的属于 s2 的字符,并输出结果。

str1 = input("请输入第一个字符串")str2 = input("请输入第二个字符串")for i in str1:    if i not in str2:        print(i,end="")

拼接最大字符

输入两个长度相等的字符串,将两字符串中相同索引中较大的字符组成一个新的字符串并输出,使用 ASCII 码来比较字符大小。

# 请使用 input() 输入两个字符串 string1, string2string1=input()string2=input()# 请分别比较两个字符串中的每一个字符,将大的字符拼接成一个新的字符串,并输出str1=list(string1)str2=list(string2)str=""for i in range(len(string1)):if str1[i]>=str2[i]:   str=str+str1[i] else:   str=str+str2[i]print(str)

单词转换

请用程序实现:输入一段英文文本 text,将文本中的单词分别按照 全部小写、全部大写 和 标题化 格式输出。

# 请使用 input() 输入一段文本 texttext = input("输入文本")# 请将文本中的单词分别按照 全部小写、全部大写 和 标题化 格式输出print("全部小写:",text.lower())print("全部大写:",text.upper())print("标题化:",text.title())

统计词量

输入一段仅由英文字母、空格组成的文本,并通过split()方法统计这段文本中的单词数量,并将统计结果输出。

# 请使用 input() 输入一断文本 texttext = input('')# 请统计这段文本中的单词数量,并将统计结果输出num = len(text.split())print(num)

回文字符串

输入一个字符串,判断它是否为回文字符串。如果是回文字符串,输出yes;如果不是回文字符串,输出no

str = input("请输入一个字符串")#逆序形成新的字符串str1str1 = str[::-1]if (str == str1):   print("yes")else:   print("no")

过滤敏感字符

请用程序实现:输入一段英文文本 text,将其中出现的敏感词改为***(三个星号),并将更改后的内容输出。

注意:敏感字符包括 “fuck”、“shit”、“bitch”,其他的暂不考虑。

# 请使用 input() 输入一段英文文本 texttext = input()# 请将文本中出现的敏感词改为`***`,并将更改后的内容输出text = text.replace('fuck','***')text = text.replace('shit','***')text = text.replace('bitch','***')print(text)

时间格式化

获取当前时间,并将获取到的时间按照年-月-日 时:分:秒的格式打印输出。

import timet = time.gmtime()print(time.strftime('%Y-%m-%d %H-%M-%S',t))

文本进度条

import timescale=50print("执行开始".center(scale//2,"-"))start=time.perf_counter()for i in range(scale+1):    a='*'*i    b='.'*(scale-i)    c=(i/scale)*100    dur=time.perf_counter()-start    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='')    time.sleep(0.1)print("\n"+"执行速度".center(scale//2,'-'))

第五章 程序控制结构

年龄换算

狗是人类的好朋友,一只狗大约可以生存 15 至 20 年,狗狗在 岁时,已经成年。为了对狗的年龄有个直观认识,有一个简单的换算办法能将狗的年龄换算为人类的年龄:

狗狗 1 岁时,相当于人类 15 岁,2 岁时相当于人类 24 岁,此后,狗狗每长 1 岁,都相当于人长 4 岁。

请用程序实现:输入狗狗的年龄,计算并输出对应的人类的年龄。

# 请使用 input() 输入狗狗的年龄age=int(input())# 计算相对人的岁数,并将结果输出if age==1:    print("15")elif age==2:    print("24")else:    ages=24+(age-2)*4    print("%d" %ages)

判断平闰年

如果一个年份可以被 4 整除且不能被 100 整除,或者可以被 400 整除,那么这个年份就是闰年。

劣:

# 请使用 input() 输入一个年份 yearyear = int(input())# 请判断这个年份是否为闰年if year%400==0:    print("leap year")else:    if year%100==0:        print("common year")    else:        if year%4==0:            print("leap year")        else:            print("common year")

优:

# 请使用 input() 输入一个年份 yearyear = int(input("输入年份"))# 请判断这个年份是否为闰年if (year%4==0 and year%100!=0) or year % 400==0:    print("leap year")else:    print("common year")

考试评级

输入一个表示考试成绩score的整数,判断该成绩属于哪个级别,并将判断结果输出。

劣:

# 请使用 input() 输入考试成绩 scorescore = int(input('请输入考试成绩: '))# 请判断成绩属于哪个级别if score<59:    print("E")elif score>=60 and score<=69:    print("D")elif score>=70 and score<=79:    print("C")elif score>=80 and score<=89:    print("B")elif score>=90 and score<=100:    print("A")

优:

# 请使用 input() 输入考试成绩 scorescore = int(input('请输入考试成绩: '))# 请判断成绩属于哪个级别if score<60:    print("E")elif score<70:    print("D")elif score<80:    print("C")elif score<90:    print("B")else:    print("A")

大小写转换

a、b、c、d这样的52个字母(包括大写)在计算机中存储时也要使用二进制数来表示。

标准ASCII码使用7位二进制数(剩下的1位二进制为0)来表示所有的大写和小写字母,如下图所示,可以看出字母对应大小写的差值为32。来进行大小写转换吧。

注意:python 中,使用 ord() 函数将 字符 转换成其对应的 ASCII 码;使用 chr() 函数将 ASCII 码转换成其对应的字符。

判断1:

# 请使用 input() 输入一个英文字母 charchar = input()# 请实现英文字母的大小写转化iford(char)>96:    char = chr(ord(char)-32)else:    char = chr(ord(char)+32)print(char)

判断2:

# 请使用 input() 输入一个英文字母 charchar=input("输入一个英文字母")if ord(char)>=ord('A'and ord(char)<=ord('Z'):    print(chr(ord(char)+32))else:    print(chr(ord(char)-32))# 请实现英文字母的大小写转化

判断位数并打印各位的值

输入一个不多于五位的正整数num,求出它是几位数,将结果输出,并将每一位的数字都打印出来。

注意: 位数和每位的数字分两行打印。

字典:

# 请使用 input() 输入一个不多于五位的正整数 numnum = input()wei = {    1'一',    2'二',    3'三',    4'四',    5'五'}# 请计算这个正整数的位数,并将每一位的数字都打印出来。print(wei[len(num)])for i in range(len(num)):    print(num[i],end=" ")    i += 1

if分支:

num=str(input("输入一个数字"))lens=len(num)if len(num)==1:    print("一")elif len(num)==2:    print("二")elif len(num)==3:    print("三")elif len(num) == 4:    print("四")elif len(num) == 5:    print("五")i=0while i<len(num):    print(num[i],end=" ")    i+=1

判断回文数

输入一个五位数,判断它是否是回文数。如果是,输出yes;如果不是,输出no

解1

num = input()a = num[:1]b = num[1:2]c = num[2:3]d = num[3:4]e = num[4:5]if a==e and b==d:    print("yes")else:    print("no")

解2

str = input("请输入一个字符串")#逆序形成新的字符串str1str1 = str[::-1]if (str == str1):   print("yes")else:   print("no")

地铁车票

输入乘坐人数(per_num)和乘坐站数(sta_num),计算购买地铁车票需要的总金额,并将计算结果输出。

注意: 如果「乘坐人数」和「乘坐站数」为0负数,输出error

少判断:

# 请使用 input() 输入乘坐的人数 per_num 和站数 sta_numper_num = int(input())sta_num = int(input())# 请判断输入的人数和站数是否正确,计算购买车票的总金额,并将计算结果输出if per_num <=0 or sta_num <= 0:    print("error")elif sta_num<5:    print(per_num*3)elif sta_num<10:    print(per_num*4)else:    print(per_num*5)

多判断:

# 请使用 input() 输入乘坐的人数 per_num 和站数 sta_num# 请判断输入的人数和站数是否正确,计算购买车票的总金额,并将计算结果输出per_num=int(input("乘坐的人数"))sta_num=int(input("站数"))if per_num<=0 or sta_num<=0:    print("error")elif sta_num<=4 and sta_num>=1:    print(per_num*3)elif sta_num <= 9 and sta_num >= 5:    print(per_num*4)elif sta_num >= 9:    print(per_num*5)

判断星期

每个星期对应的英语单词都不同,星期一到星期天的单词分别为:monday、tuesday、wednesday、thursday、friday、saturday、sunday。请用程序实现

输入单词的前两个字符,判断输入的单词是星期几,并输出对应的单词;如果输入的字符不匹配,则输出error

字典:

# 请使用 input() 输入单词的前两个字母 charschars = input()pd = {    'mo''monday',    'tu''tuesday',    'we''wednesday',    'th''thursday',    'fr''friday',    'sa''saturday',    'su''sunday',}   # 请判断输入的是星期几,并输出对应的单词if chars in pd.keys():    print(pd[chars])else:    print("error")

if分支:

# 请使用 input() 输入单词的前两个字母 charschars = input()# 请判断输入的是星期几,并输出对应的单词if chars=='mo':    print("monday")elif chars=='tu':    print("tuesday")elif chars=='we':    print("wednesday")elif chars=='th':    print("thursday")elif chars=='fr':    print("friday")elif chars=='sa':    print("saturday")elif chars=='su':    print("sunday")else:    print("error")

身体质量指数BMI

BMI :Body Mass Index 国际上常用的衡量人体肥胖和健康程度重要标准,主要用于统计分析‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬。BMI 的计算公式为 BMI = 体重(kg) / (身高(m) * 身高(m))。请用程序实现

获取用户输入的体重和身高值,计算并给出国际和国内的 BMI 分类。

嵌套if:

height = float(input())weight = float(input())# 计算bmi值并将判断结果输出bmi = weight / height**2print("BMI数值为:%.2f"%bmi)if bmi<18.5:    a = b = '偏瘦'elif bmi<25:    a = '正常'    if bmi<24:        b = a    else:        b = '偏胖'elif bmi<30:    a = '偏胖'    if bmi<28:        b = a    else:        b = '肥胖'else:    a = b = '肥胖'print("BMI指标为:国际'%s', 国内'%s'"%(a,b))

多if:

height = float(input())weight = float(input())BMI=weight/pow(height,2)print(f"BMI数值为:{BMI:.2f}")A='s'B='s'if BMI<18.5:    A=B='偏瘦'elif 18.5<=BMI<25:    A='正常'elif 25<=BMI<30:    A='偏胖'elif BMI>=30:    A='肥胖'if 18.5<=BMI<24:    B='正常'elif 24<=BMI<28:    B='偏胖'elif BMI>=28:    B='肥胖'print(f"BMI指标为:国际'{A}',国内'{B}'")

乞丐

丐帮帮主去天桥乞讨,并把每天乞讨的钱都存起来。设帮主存款初始为 0,且不使用这笔钱。

第一天乞讨了 1 块钱;第二天乞讨了 2 块钱;第三天乞讨了 4 块钱;第四天乞讨了 8 块钱;以此类推。

# 请使用 input() 输入一个天数 dayday = int(input("请输入一个天数: "))# 计算帮主这些天的总收入,并将每天的总收入输出for i in range(1,day+1):    s = 2 ** i - 1    print(s)

水仙花数

"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如 153 = 1³ + 5³ + 3³,所以 153 是一个水仙花数。

请用程序实现,输入一个三位数,找出100~num(含)中的所有水仙花数,并将找出的水仙花数从小到大输出,每行输出1个数

for循环:

# 请使用 input() 输入一个三位数 numnum = int(input('请输入一个三位数: '))# 请找出 100 - num(含) 中的所有水仙花数,并将找出的水仙花数输出for i in range(100,num+1):    a = int(i/100)    b = int(i/10)%10    c = i%10    if i == c**3+b**3+a**3:        print(i)

while循环:

# 请使用 input() 输入一个三位数 numnum = int(input('请输入一个三位数: '))# 请找出 100 - num(含) 中的所有水仙花数,并将找出的水仙花数输出s=100while s<=num:    ge=s%10    shi=s//10%10    bai=s//100    if pow(ge,3)+pow(shi,3)+pow(bai,3)==s:        print(s)    s+=1

篮球弹跳

篮球从一定高度向下掉落,每一次弹起的高度,都是前一次高度的一半。一次掉落和一次弹起极为一次弹跳。假设篮球初始高度为10米。请用程序实现

输入篮球弹跳的次数num,计算num次后篮球所在的高度,并将计算结果输出。

# 请使用 input() 输入弹跳的次数 numnum=int(input("请输入弹跳的次数: "))result=10for i in range(0,num):    result=result/2print(result)# 请计算弹跳 num 次后的篮球高度,并将结果输出

阶乘

请用程序实现,输入一个正整数num,计算这个正整数的阶乘,并将计算结果输出。

# 请使用 input() 输入一个正整数 numnum = int(input('请输入正整数: '))# 请计算这个正整数的阶乘,并将计算结果输出sum = 1if num == 0:    print("0的阶乘是1")elif num < 0:    print("负数没有阶乘")else:    for i in range(1,num + 1):        sum = sum * i    print(sum)

猴子摘桃

一只猴子摘桃子, 第一天摘了两个桃, 以后每天摘的是前一天的两倍还多一个.

请用程序实现,输入一个天数day,计算第day天它摘的个数,并将结果输出。

# 请使用 input() 输入一个天数 dayday = int(input('请输入一个天数: '))# 请计算第 day 天猴子摘的桃子个数,并将结果输出i=2res=2while i<=day:    res=2*res+1    i+=1print(res)

冰雹猜想

输入一个大于1的正整数num; 如果num为偶数, 就将它变为num / 2; 如果num为奇数, 就将它变为3 * num + 1; 直到num1时停止。并将每一次运算的结果输出。

# 请使用 input() 输入一个整数 numnum = int(input("请输入一个整数: "))# 编写程序验证冰雹猜想while num !=1:    if num%2==0:            num = num // 2            print(num)    else:            num = 3*num+1            print(num)

忽略倍数

输入一个整数num,循环打印1(含)~num(含)中的整数,如果是3的倍数或5的倍数,则忽略。

# 请使用 input() 输入一个整数 numnum = int(input('请输入一个整数: '))# 请打印 1(含) - num(含) 中的整数,如果是 3 的倍数或 5 的倍数,则忽略for i in range(1,num+1):    if i%3!=0 and i%5!=0        print(i)  

素数

「质数」又称素数,有无限个。素数定义为在大于 1 的自然数中,除了 1 和它本身以外不再有其他因数的数称为素数。

例如17就是素数,因为它不能被2 - 16的任一整数整除。

请用程序实现: 输入一个大于 1 的整数num,输出1~num(含)中的所有的素数(每行输出1个素数,从小到大排列)

少语句:

# 请使用 input() 输入一个整数 numnum = int(input('请输入一个整数: '))j = 1# 输出 1 - num(含) 中的所有的素数for i in range(num+1):    for j in range(2,i):        if i%j==0:            break;    if j==i-1:        print(i)

增加判定:

请使用 input() 输入一个整数 numnum = int(input('请输入一个整数: '))for i in range(2,num+1):    fg=0    for j in range(2,i-1):        if i%j==0:            fg=1            break    if fg==0:        print(i)# 输出 1 - num(含) 中的所有的素数

九九乘法表

str输出:

# 请在此处编写你的程序for i in range(1,10):    for j in range(1,i+1):        print(str(j)+'x'+str(i)+'='+str(i*j),end=' ')    print()

%输出:

# 请在此处编写你的程序for i in range(1,10):    for j in range(1,i+1):        print('%sx%s=%s'%(i,j,i*j),end = " ")    print()

随机密码生成

‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬以整数 17 为随机数种子,获取用户输入整数 N 为长度,产生 3 个长度为 N 位的密码,密码的每位是一个数字。每个密码单独一行输出。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

注意: 产生密码采用random.randint()函数。

# 请在...补充代码import randomdef genpwd(length):    a = 10 ** (length - 1)    b = 10 ** length - 1    return "{}".format(random.randint(a,b))length = eval(input())#seed() 方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数。random.seed(17)#设置该种子后 每次随机出现的序列都是相同的for i in range(3):    print(genpwd(length))

猜数字

使用 python 实现猜数字游戏,即系统随机生成一个整数,通过 input() 函数,输入整数,根据系统给出的提示猜数字。

要求:

  • 随机生成一个 1 - 100 之间的整数。
  • 7 次猜数字的机会,如果没有猜正确,游戏结束。
  • 输入字母 q,则退出游戏。
# 导入random模块import random# 生成随机数,并赋值给num变量num = random.randint(1100)# 定义 guess_chances 变量,初始化猜的次数guess_chances = 7print('您只有7次猜数字的机会哦!')# 循环输入的次数for i in range(1, guess_chances + 1):    print('这是第' + str(i) + '次猜数字')    # 实现对输入数字的判断    guess=input("请输入数字:")    if guess.isdigit():        guess=int(guess)        if guess<num:            print('您输入的数字太小了,您还有'+str(guess_chances-i)+'次机会,请重新输入!')        elif guess>num:            print('您输入的数字太大了,您还有'+str(guess_chances-i)+'次机会,请重新输入!')        elif guess==num:            print('恭喜您猜对了!')            break        elif guess=='q':            print("退出游戏")            break        else:            print("输入的内容必须为整数")

圆周率的计算

求解圆周率可以采用蒙特卡罗方法,在一个正方形中撒点,根据在 1/4 圆内点的数量占总撒点数的比例计算圆周率值。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

请用程序实现:请以 123 作为随机数种子,获得用户输入的撒点数量,编写程序输出圆周率的值,保留小数点后 6 位。

解1

#调用random函数,并且使用了perf_counter这个函数,是可以用来计时的一部分from random import randomfrom time import perf_counter#定义变量,当作抛洒点的总数量DARTS = 1000 * 1000#撒在圆内部点为0hits = 0.0start = perf_counter()for i in range(1,DARTS+1):    x,y = random(),random()    dist = pow(x ** 2 + y ** 2,0.5)    if dist <= 1.0:        hits = hits + 1pi = 4 * (hits / DARTS)print("圆周率值是:{}".format(pi))print("运行时间是:{:.5f}s".format(perf_counter() - start))

解2

import randomm = int(input())# 统计落在圆内点的数量n = 0random.seed(123)for i in range(m):    # 随机生成-1到1之间的小数    x = random.uniform(-1,1)    y = random.uniform(-1,1)    # 生成的点到圆心之间的距离    len = pow(x**2+y**2,0.5)    # 生成的点在圆内    if len < 1:        n += 1#由于圆面积/正方形面积 = π/4 即π等于4*正方形面积 pi = 4*(n/m)print("%.6f"%pi)

求π的近似值

输入精度 e,使用格雷戈里公式求 π 的近似值,精确到最后一项的绝对值小于 e.

输入格式:输入在一行中给出精度 e

输出格式:对于给定的输入,在一行中输出π的近似值。

解1

e = float(input())# 请根据 e 计算 pi 的近似值x=-1n=2result=1while(1/(2*n-3)>=e):    result = result+x*(1/(2*n-1))    x=x*(-1)    n=n+1count=4*resultprint(count)

解2

e = float(input())# 请根据 e 计算 pi 的近似值x = 0n = 1while True:      #不断循环去找出条件符合的结果    x = x + pow(-1,n + 1) * (1/(2 * n - 1))   #格雷戈里公式    if(e > abs(1/(2 * n - 1))):        break    #满足条件跳出循环    else:        n += 1print(x*4)

解3

e = float(input())#请根据e计算pi 的近似值sum = 0item1time = 1while item >= e:    item = (1.6 / (2 *time - 1))    sum += (1 if time % 2 != 0 else -1)*itemtime += 1pi = sum *4print(pi)

第六章 函数和代码复用

打招呼函数

用函数给某个人打招呼。请调用函数测验函数代码逻辑是否正确,不要获取键盘输入值。

"""打招呼函数要求:1. 定义函数 say_hello2. 有 1 个参数 name 表示要给谁打招呼3. 实现函数功能,即在控制台打印:`你好,<name>,认识你很高兴!`(注:name 是函数的参数)"""def say_hello(name):    print("你好,{},认识你很高兴!".format(name))say_hello("张三")

能否组成三角形函数

判断三条线段能否构成一个三角形,需要满足两条规则:

  • 三角形的三条边长必须大于零。
  • 任意两边之和必须大于第三边。

用函数判断三个数字能否构成三角形,并将判断结果返回。

参数说明 abc均为整数。

"""编写 is_triangle 函数,此函数有 3 个参数,分别为3个数字,判断这3个数字所代表的边长能否组成一个三角形"""def is_triangle(a, b, c):    """请实现函数"""    if(a<=0 or b<=0 or c<=0):        return -1    else:        if a+b>c and a+c>b and b+c>a:            return 1        else:            return 0    # 调用函数验证三边能否组成三角形    print(is_triangle(345))

计算n个自然数的立方和

编程实现:定义一个函数,其功能是计算 n 个自然数的立方和,如下图所示。

  1. 定义函数 sumOfSeries
  2. 有 1 个参数 n 表示自然数的个数
  3. 有 1 个返回值,返回自然数的立方和
  4. 调用函数,使函数执行后在控制台打印:‘和为:xx’
# 定义立方和的函数def sumOfSeries(num):    sum = 0    for i in range(1,num+1):        sum+= pow(i,3)    return sum# 调用函数print("和为:",sumOfSeries(5)) 

简单计算器实现

编程题

请用程序实现

用函数实现 加、减、乘、除 的运算。

参数说明

num1,num2均为整数,表示需要进行计算的两个数字。

返回值说明

四个函数的返回值,均为执行对应操作后计算出来的值。请调用函数测验函数代码逻辑是否正确,不要获取键盘输入值。

# 定义加法函数 additiondef addition (num1, num2):    return num1+num2# 定义减法函数 subtractiondef subtraction (num1, num2):    return num1-num2# 定义乘法函数 multiplicationdef multiplication (num1, num2):    return num1*num2# 定义除法函数 divisiondef division (num1, num2):    return num1/num2print(addition(34))print(subtraction(34))print(multiplication(34))print(division(124))

转换秒为时间

编程题

1 天有 86400 秒,那么 100000 秒相当于多长时间呢?

编程实现:完善函数convert_from_seconds,将一个指定的秒数转换为[天, 时, 分, 秒]的数据格式,并将其返回。请调用函数测验函数代码逻辑是否正确,不要获取键盘输入值。

解1

# 定义一个 convert_from_seconds 函数, 参数 seconds, 返回表示时间的列表def convert_from_seconds (seconds):    day = seconds // 86400    hour = seconds % 86400  // 3600    minutes = seconds// 60 % 60    s = seconds % 60    return [day,hour,minutes,s]

解2

#解2def convert_from_seconds1 (seconds):    #day,h,m,s    int(seconds)    day=seconds//86400    h=(seconds-day*86400)//3600    m=(seconds-day*86400-h*3600)//60    s=(seconds-day*86400-h*3600-m*60)    return [day,h,m,s]print(convert_from_seconds(100000))

最大公约数

编程题

编程实现:按下面的要求,输出两个正整数的最大公约数。

  • 定义函数 common_divisor(num1, num2),该函数返回两个正整数的最大公约数;函数有两个参数,分别对应两个正整数;有1个返回值,代表求得的最大公约数。
  • 接收两个输入值,调用函数 common_divisor,输出最大公约数。
# 定义并实现函数 common_divisordef common_divisor(num1, num2):    if num1 % num2 ==0:        return num2    else:        if num1>num2:            return common_divisor(num2,num1%num2)        elif num1<num2:            return common_divisor(num2,num1)# 获取输入值,调用函数,输出最大公约数num1=int(input("请输入第一个数"))num2=int(input("请输入第二个数"))result = common_divisor(num1, num2)print(result)

杨辉三角

编程题

杨辉三角,又称贾宪三角形、帕斯卡三角形,是二项式系数在三角形中的一种几何排列。以下是杨辉三角的前十行:

                  1                1   1              1   2   1            1   3   3   1          1   4   6   4   1        1   5   10  10  5   1      1   6   15  20  15  6   1    1   7   21  35  35  21  7   1  1   8   28  56  70  56  28  8   11   9   36  84  126 126 84  36  9   1

由上可以看出:

  1. 每行端点与结尾的数为 1
  2. 每个数等于它上方两数之和
  3. 每行数字左右对称,且由 1 开始逐渐变大
  4. 第 n 行的数字有 n 项
# 定义函数 pascal_triangle 接受参数 num,并返回杨辉三角第 num 行def pascal_triangle(num):    r=[1]    for i in range(1,num):        r.insert(0,0)        for j in range(i):            r[j] = r[j]+r[j+1]    return r# 调用函数result = pascal_triangle(3)print(result)

线性查找

编程题

线性查找指按一定的顺序检查列表中每一个元素,直到找到所要寻找的特定值为止示。

编程实现:定义一个函数,其功能是线性查找列表中的元素,如下图所示。请按下面的要求实现。

  • 定义线性查找函数 linear_searching(list,size,target),接收三个参数  要进行查找的列表(list)、列表长度(size)、目标元素(target),在 list 中查找 target,如果 target 在  list 中则将 target 的位置(索引+1)返回;否则返回数字 -1。
  • 列表元素已给出,接收输入的目标元素值,调用函数 linear_searching,输出查找结果,如果查到,则输出“该元素在第几个位置”;如果未查到,则输出“该元素不在列表
# 定义函数 linear_searching 接受参数 list, size, target,并将查找结果返回def linear_searching(list,size,target):    for i in range(size):        if list[i]==target:            return i+1    return -1# 输入目标元素值,调用函数,输出结果l=[1,2,3,12,-10]size = len(l)target = int(input("输入目标元素"))index = linear_searching(l,size,target)if index == -1:    print("该元素不在列表中")else:    print("该元素在第{}个位置".format(index))

七段数码管绘制

编程题

七段数码管是一种展示数字的有效方式。

(1) 使用 time  库获得系统当前时间,格式如下:20190411‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

(2) 绘制对应的七段数码管‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

(3) 数码管风格不限‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

import turtledef drawLine (draw):    turtle.pendown() if draw else turtle.penup()    turtle.fd(40)    turtle.right(90)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)def drawDate (date):    for i in date:        drawDigit (eval(i))def main ():    turtle.setup(800,350)    turtle.penup()    turtle.fd(-300)    turtle.pensize(5)    drawDate('20200609')    turtle.hideturtle()    turtle.done()main()

斐波那契数列计算

编程题

形如1,1,2,3,5,8......的数列,被称之为斐波那契数列。这个数列的特点是从第三个数字开始,每个数字都等于前两个数字之和。

请用程序实现

用函数实现,计算斐波那契数列某项的值,并将计算结果返回。

函数定义

def fbi (num):    pass

num是一个整数,表示斐波那契数列的项数。

返回值说明

函数返回一个整数,该整数为斐波那契数列第 num 项的值。

# 定义一个 fbi 函数,参数 num,返回斐波那契数列第 num 项的值。def fbi(num):    if num==1:        return 1    elif num==2:        return 1    else:        return fbi(num-2)+fbi(num-1)# 调用函数print(fbi(20))

汉诺塔实践

编程题

请用程序实现

用函数实现汉诺塔的移动步骤拆解。

函数定义

def hanoi (n, src, dst, mid):    pass

参数说明

  • n
    是整数,表示圆柱 A 上面的圆盘个数。
  • src
    是大写字母A,表示最左边的圆柱。
  • dst
    是大写字母C,表示最右边的圆柱。
  • mid
    是大写字母B,表示中间的圆柱。

返回值说明

此函数没有返回值,调用函数后函数输出圆盘移动的步骤。

# 请在...补充一行或多行代码count = 0def hanoi (n, src, dst, mid):    global count    if n == 1:        count += 1        print("{}: {}->{}".format(count, src, dst))    else:        hanoi(n-1,src,mid,dst)        count += 1        print("{}: {}->{}".format(count, src, dst))        hanoi(n-1, mid, dst, src)hanoi(3"A""C""B")print(count)

合法的用户名

编程题

有一些网站注册用户时,会对用户名长度进行限制,比如要求用户名的长度必须在6(含)~18(含)位之间。

请用程序实现

用函数实现对注册用户名的合法性进行检查。请调用函数测验函数代码逻辑是否正确,不要获取键盘输入值。

函数定义

def check_username (username):    pass

参数说明

username是一个任意长度的字符串,表示待检查的用户名。

返回值说明

如果用户名长度在6(含)~18(含),则用户名有效,返回True;否则返回False

"""实现 check_username 函数,检查 username 是否有效username 长度在 6-18 位之间,返回 True,否则返回 False"""def check_username(username):    length = len(username)    if length>=6 and length<=18:        return True    else:        return False# 调用函数result = check_username('lisi')print(result)

科赫雪花小包裹

编程题

科赫曲线,也叫雪花曲线。绘制科赫曲线。

请补充编程模板中代码,完成功能:获得用户输入的整数level,作为阶,绘制level阶科赫曲线。

# 请在...补充一行或多行代码import turtledef 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(level):    turtle.setup(600600)    turtle.penup()    turtle.goto(-200100)    turtle.pendown()    turtle.pensize(2)    #level=2    koch(400,level)    turtle.right(120)    koch(400,level)    turtle.right(120)    koch(400,level)    turtle.hideturtle()try:    level = eval(input("请输入科赫曲线的阶: "))    main(level)except:    print("输入错误")

密码的强度

编程题

密码是账户的重要安全保障,涉及到安全问题,太简单的密码容易被猜到或破解。

请用程序实现

用函数实现一个校验密码强度的函数,用于提醒用户在注册时,密码是否足够安全。请调用函数测验函数代码逻辑是否正确,不要获取键盘输入值。

以下为密码强度校验规则:

  1. 密码长度在 6 位及以上,强度 +1,在 8 位及以上,强度 +2,12 位及以上,强度 +4
  2. 大写字母,强度 +2
  3. 除字母外,还包含数字,强度 +2
  4. 除字母、数字以外字符强度 +2
"""实现密码强度计算函数:1. 实现函数 passworld_strength 返回 0-10 的数值,表示强度,数值越高,密码强度越强2. 密码长度在 6 位及以上,强度 +1,   在 8 位及以上,强度 +2,   在 12 位及以上,强度 +43. 有大写字母,强度 +24. 除字母外,还包含数字,强度 +25. 有除字母、数字以外字符,强度 +2"""def password_strength(pwd):    intensity=0    if len(pwd)>=12:        intensity+=4    elif 8<=len(pwd)<12:        intensity+=2    elif 6<=len(pwd)<8:        intensity+=1    pwdlist=list(pwd)    for i in range(len(pwd)):        if 'A'<=pwdlist[i]<='Z':            intensity+=2            break    for i in range(len(pwd)):        if 'A'<=pwdlist[i]<='Z' or 'a'<=pwdlist[i]<='z':            for j in range(len(pwd)):                if '0'<=pwdlist[j]<='9':                    intensity+=2                    break        break    for i in range(len(pwd)):        if ('null'<=pwdlist[i]<'0'or ('9'<pwdlist[i]<='@'or ('Z'<pwdlist[i]<='`'or ('z'<pwdlist[i]<='~'):            intensity+=2            break    return intensitypwd1=str(input())print(password_strength(pwd1))

藏头诗

poem1 = [    "芦花丛中一扁舟",    "俊杰俄从此地游",    "义士若能知此理",    "反躬难逃可无忧"]poem2 = [    "我画蓝江水",    "爱晚亭上枫",    "秋月溶溶照",    "香烟袅袅绕"]def acrostic(poems):    string''    for poem in poems:        string+=poem[0]    return stringprint(acrostic(poem1))print(acrostic(poem2))

统计指定字符出现次数函数

编程题

用函数实现,统计字符串中指定字符出现的次数。请调用函数测验函数代码逻辑是否正确,不要获取键盘输入值。

解1

"""统计字符串 string 中出现字符 char 的次数,并返回;char 是长度为 1 的字符串。"""def sum_char(string, char):    k = 0    for i in range(len(string)):        if char == string[i]:            k = k+1    return kprint(sum_char('hello,world','o'))

解2

描述

Python count() 方法用于统计字符串里某个字符或子字符串出现的次数。可选参数为在字符串搜索的开始与结束位置。

语法

count()方法语法:

str.count(sub, start= 0,end=len(string))

参数

  • sub – 搜索的子字符串
  • start – 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
  • end – 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。

返回值

该方法返回子字符串在字符串中出现的次数。

str = "hello_world"char = 'l'print(str.count(char))

文件扩展名

编程题

文件扩展名是操作系统用来标记文件类型的一种机制。通常来说,一个扩展名是跟在主文件名后面的,由一个分隔符(.)分隔。

请用程序实现

用函数实现,将文件的扩展名获取出来。

函数定义

def file_ext (filename):    pass

参数说明

filename是一个字符串,表示文件名。

返回值说明

如果 filename 有扩展名,则函数返回该 filename 的扩展名,否则返回文件名错误。请调用函数测验函数代码逻辑是否正确,不要获取键盘输入值。

"""获取文件扩展名说明:实现 file_ext 函数,该函数接受一个表示文件名的字符串参数 filename,返回它的扩展名"""def file_ext(filename):    for i in range(len(filename)):        if filename[i] == '.':            return filename[i+1:]filename = input()print(file_ext(filename))

插入排序

编程题

插入排序(英语:Insertion Sort)是一种简单直观的排序算法。

它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。

定义一个函数,其功能是进行插入排序,如下图所示。请编写函数完成要求。

# 定义函数 insertion_sort 接受参数 list_sort,并返回插入排序结果。def insertion_sort(list_sort):    for i in range(len(list_sort)):        j = i         # 和有序表中的每个一样元素进行比较 从最后一个开始        while j > 0:            if list_sort[j] < list_sort[j-1]:                list_sort[j],list_sort[j-1] = list_sort[j-1],list_sort[j]                j -= 1            else:                break    return list_sortlist_sort = [9,8,8,7,6,5,4,3,2,1]print(insertion_sort(list_sort))

选择排序

编程题

选择排序(Selection sort)是一种简单直观的排序算法。

它的工作原理如下。首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,

然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

# 定义函数 selection_sort 接受参数 list_sort,并返回选择排序结果。def selection_sort(list_sort):    for i in range(len(list_sort)):        for j in range(i,len(list_sort)):            if list_sort[i] > list_sort[j]:                    list_sort[i], list_sort[j] = list_sort[j], list_sort[i]    return list_sort             list_sort = [987654321]print(selection_sort(list_sort))            

冒泡排序

编程题

冒泡排序(Bubble Sort)也是一种简单直观的排序算法。

它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。

走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢"浮"到数列的顶端。

# 定义函数 bubble_sort 接受参数 list_sort,并返回冒泡排序结果def bubble_sort(list_sort):    n = len(list_sort)    for i in range(n):        for j in range(0,n-i-1):            if list_sort[j] > list_sort[j+1]:                    list_sort[j], list_sort[j+1] = list_sort[j+1], list_sort[j]    return list_sortsort = [2,4,2,8,4,6,1]print(bubble_sort(sort))

二分查找

编程题

二分搜索是一种在有序数组中查找某一特定元素的搜索算法。

搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;

如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组为空,则代表找不到。

这种搜索算法每一次比较都使搜索范围缩小一半。

定义一个函数,其功能是进行二分查找,如下图所示。请编写函数完成要求。

# 定义函数 binary_search 接受参数 list_sort, size, targer,并将查找结果返回def binary_search(list_sort, size, targer):    middle = size//2    if targer == list_sort[middle]:        return middle    elif targer>=list_sort[middle]:        for i in range(middle,size):            if targer == list_sort[i]:                return i            return -1    else:         for i in range(0,middle):            if targer == list_sort[i]:                return i            return -1list_sort =[1,2,3,5,6,7,8]print(binary_search(list_sort,7,6))

第七章 组合数据类型

集合运算

程序中已经存在 manager 集合存储公司所有的经理名字,technician 集合存储所有的技术员的名字。

请编写代码按照下面的要求,生成新的集合。

  1. 将既是经理又是技术员的职员保存到集合 set1 中,并输出
  2. 将是经理但不是技术员的职员保存到集合 set2 中,并输出
  3. 将是技术员但不是经理的职员保存到集合 set3 中,并输出
  4. 将身兼一职的职员保存到集合 set4 中,并输出
  5. 判断 张飞 是否是经理,如果是经理,输出「yes」;不是输出「no」
  6. 求经理和技术员有几人,并将结果输出
# 将既是经理又是技术员的职员保存到集合 set1 中,并输出set1 = manager & technicianprint(set1)# 将是经理但不是技术员的职员保存到集合 set2 中,并输出set2 = manager - technicianprint(set2)# 将是技术员但不是经理的职员保存到集合 set3 中,并输出set3 = technician - managerprint(set3)# 将身兼一职的职员保存到集合 set4 中,并输出set4 = manager ^ technicianprint(set4)# 判断 张飞 是否是经理set5 = {"张飞"}if "张飞" in manager:    print("「yes」")else:    print("「no」")print(set5)# 求出经理和技术员有几人,并将结果输出print(len(manager | technician))# |去除了两个集合之中重复的元素 只保留1个

集合的关系

请通过集合操作符判断集合 set1set2的子集关系,具体要求如下:

  1. set1set2,则输出set1是set2的子集;
  2. set1>set2,则输出set1包含set2;
  3. set1=set2一样,则输出set1和set2互为子集。
  4. 否则输出 set1和set2无关

解1

# 创建两个集合set1 = {23456}set2 = {246}# 判断两集合关系if set1 < set2:    print("set1是set2的子集")elif set1 > set2:    print("set1包含set2")elif set1 == set2:    print("set1和set2互为子集")else:    print("ser1和set2无关")

解2

#issubset() 方法用于判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False。set1 = {23456}set2 = {246}if (set1.issubset(set2) and set2.issuperset(set1)) or set1 == set2 == null:    print('set2是set1的真超集,set1是set2的真子集')elif set2.issubset(set1) and set1.issuperset(set2):    print('set1是set2的真超集,set2是set1的真子集')elif set1 == set2:    print('set1和set2互为子集')else:    print('set1和set2没有关系')

集合综合练习

已知某学校的某次考试成绩分班级保存在集合 score1score2score3score4score5 中,5个集合都已存在,可直接使用。

请用集合解决以下问题:

  1. 突然发现 score1 的数据格式为列表,请将 score1 转换为集合,并将集合 score1 输出
  2. 使用 add() 方法将数据 10 添加到集合 score2 中,并将集合 score2 输出
  3. 使用 update() 方法将数据 {11, 12, 13} 添加到集合 score3 中,并将集合 score3 输出
  4. 使用 remove() 方法将数据 83 从集合 score4 中删除,并将集合 score4 输出
  5. 使用 clear() 方法将集合 score5 中的数据清空,并将集合 score5 输出
# 将 score1 转换为集合score1 = set(score1) print(score1)# 将 10 添加到 score2 中score2.add(10)print(score2)# 将 {11, 12, 13} 添加到 score3 中score3.update({11,12,13})print(score3)# 将 83 从 score4 中删除score4.remove(83)print(score4)# 将 score5 清空score5.clear()print(score5)

添加元素方法

add  元素 x 添加到集合 s 中,如果元素已存在,则不进行任何操作

update  添加元素,且参数可以是列表,元组,字典等

移除元素方法

**remove **元素 x 从集合 s 中移除,如果元素不存在,则会发生错误

discard 如果元素不存在,不会发生错误

pop 随机删除集合中的一个元素  pop 方法会对集合进行无序的排列,然后将这个无序排列集合的左面第一个元素进行删除。

元组元素抓7

请用程序实现:找出元组 tuple_pre 中元素是 7 的倍数或个位为 7 的数,并将计算结果输出。

注意:元组 tuple_pre 已存在,可以直接使用 tuple_pre。

解1

tuple_pre = (123456789101112131415161718192021222324252627282930)# 请计算元组 tuple_pre 中元素是7的倍数及个位为7的数,并将计算结果输出for i in range(len(tuple_pre)):    if tuple_pre[i] % 7 == 0 or tuple_pre[i] % 10 == 7:        print(tuple_pre[i])

解2

tuple_pre = (123456789101112131415161718192021222324252627282930)# 请计算元组 tuple_pre 中元素是7的倍数及个位为7的数,并将计算结果输出for i in tuple_pre:    if i % 7 == 0 or i % 10 == 7:        print(i)

元组解包

“*” 的使用。在对一个元组进行解包时,变量的数量必须和元组中的元素数量一致,也可以在变量前边添加一个 * 号,这样变量将会获取元组中所有剩余的元素。

my_tuple = 10, 20, 30, 40# 在对一个元组进行解包时,变量的数量必须和元组中的元素的数量一致# 也可以在变量前边添加一个*,这样变量将会获取元组中所有剩余的元素a, b, *c = my_tupleprint('1. a =', a, 'b =', b, 'c =', c)a, *b, c = my_tupleprint('2. a =', a, 'b =', b, 'c =', c)*a, b, c = my_tupleprint('3. a =', a, 'b =', b, 'c =', c)a, b, *c = [1, 2, 3, 4, 5, 6, 7]print('4. a =', a, 'b =', b, 'c =', c)a, b, *c = 'hello world'print('5. a =', a, 'b =', b, 'c =', c)# 不能同时出现两个或以上的*变量# *a , *b , c = my_tuple

升序降序

编写程序,将列表中的前10个元素升序排列,后10个元素降序排列,并输出结果。

解1

# 打印输出 list_preprint(list_pre)# 请对上面列表中的元素进行排序list_pre[:10] = sorted(list_pre[:10])list_pre[10:] = sorted(list_pre[10:],reverse=True)print(list_pre)

解2

# 打印输出 list_preprint(list_pre)# 请对上面列表中的元素进行排序list1 = list_pre[0:10]list1.sort()list2 = list_pre[-10:]list2.sort(reverse=True)print(list1+list2)

截取部分元素

将列表list_pre中的部分元素截取到列表list_new中。

在两行中分别输入两个非负整数,表示需要截取的索引段,且第一个数小于第二个数。

list_pre = [23754533642224767018354419687549]# 请使用 input() 输入两个正整数 beginendbegin = int(input('请输入第一个索引: '))end = int(input('请输入第二个索引: '))# 将列表 list_pre 中下标从 begin(含) 到 end(含) 的元素截取到列表 list_new 中,并将 list_new 输出list_new = list_pre[begin:end+1]print(list_new)

判断元素是否在列表中存在

请用程序实现:判断指定元素是否存在于列表 list_test 中。

注意:要求分别用 for 循环与 in 关键字完成练习,列表 list_test 已存在,可以直接使用 list_test。

# 打印输出 list_testprint(list_test)# 使用for循环判断元素 4 是否存在于 list_test 列表中print("查看 4 是否在列表中 ( 使用循环 ) : "for i in list_test:     if(i == 4) :         print ("存在"# 使用in关键字判断元素 4 是否存在于 list_test 列表中print("查看 4 是否在列表中 ( 使用 in 关键字 ) : "if 4 in list_test:     print ("存在"

头尾对调

请用程序实现:将列表 list_ht 中的头尾两个元素对调,并将对调的结果输出。

# 打印输出 list_htprint(list_ht)# 将列表 list_ht 中的头尾两个元素对调,并将对调的结果输出list_ht[0],list_ht[-1] = list_ht[-1],list_ht[0]print(list_ht)

翻转列表

请用程序实现,将列表 list_reverse 翻转,并将翻转后的列表输出。

# 打印输出 list_reverseprint(list_reverse)# 将列表翻转list_reverse.reverse()print(list_reverse)

指定元素对调

请用程序实现,将列表list_swap 中所指定的元素对调到列表的尾部,并输出对调后的列表,如下图所示。

# 打印输出 list_swapprint(list_swap)# 将列表 list_swap 中的第1、2个元素对调到列表的尾部for i in range(2):    temp = list_swap[0]    for i in range(6):        list_swap[i] = list_swap[i + 1]    list_swap[6] = tempprint(list_swap)

约瑟夫生者死者小游戏

编程题

一条船上有 num 个人(num为偶数),超载了,需要一半的人下船。于是人们排成一排,排队的位置即为他们的编号。他们从头开始从 1 开始报数,数到某个数字的人下船,并且下一个人又从 1 开始报数,如此循环,直到船上只有原来一半人为止。

请用程序实现:输入表示船上总人数的偶数 num 和需要下船的特殊数字 special_num,输出下船人的编号。

num = int(eval(input("请输入船上的总人数:")))special_num = int(eval(input("请输入需要下船的特殊数字:")))people = []for i in range(1,num+1):    people.append(i)longth= int(num/2)repo = 1while(len(people)>longth):    if repo != special_num:        pp = people.pop(0)        people.append(pp)    else:        pp = people.pop(0)        print(pp)        repo = 0    repo = repo + 1

计算列表元素之和

已存在定义好的列表list_sum,请计算列表list_sum中的所有元素之和,并将计算结果输出。

# 请计算列表 list_sum 中的所有元素之和,并将计算结果输出sum = 0for i in list_sum:    sum = sum+iprint(sum)

计算总分和平均分

小明刚结束期末考试,他将自己各科的分数保存到了列表scores中,现在,请帮他进行如下计算:

  • 计算总分,并将计算结果保存到变量total_score
  • 计算平均分,并将计算结果保存到变量avg_score
# 小明的期末考试成绩分数如下:scores = [95, 69, 98, 74, 64, 72, 53, 92, 83]# 请计算小明的总分、平均分,并保存至变量 total_score, avg_score 中total_score=0for i in scores:    total_score += iavg_score = total_score/9print(total_score)print(avg_score)

添加用户

users = {    "alpha""alpha123",    "beta""betaisverygood",    "gamma""1919191923"}users["zhangsan"] ="zs123456"users["lisi"] = "si123456"

模拟用户登录

用键盘模拟用户输入,判断输入的用户名或密码是否正确,并输出登录信息。

  • 如果输入的用户名存在,且密码正确,则输出success
  • 如果输入的用户名存在,但密码不正确,则输出password error
  • 如果输入的用户名不存在,则输出not found
users = {    "alpha""alpha123",    "beta""betaisverygood",    "gamma""1919191923",    "zhangsan""123456",    "lisi""123456",    "admin""ADMIN",    "root""Root123"}username = input()password = input()if username not in users:    print("not found")elif users[username] != password:    print("password error")else:    print("success")

第八章 文件和数据格式化

读取文件并显示不以 # 开头的所有行

读取当前目录下的 data.txt 文件,显示除了以 # 开头的行以外的所有行

# 读取 data.txt 并将其中非井号(#)开头的行打印到控制台标准输出中#创建一个空的列表new = []#文件以只读方式打开text = open('data.txt','r')#从文件读出所有行赋给text列表contents = text.readlines()#遍历contents列表for i in contents:    #若列表内容开始不为#    if i.startswith('#') == False:        #将列表内容添加到new列表中去        new.append(i)#关闭文件 释放文件使用授权text.close()#将序列中的元素以指定的字符(换行符)连接生成一个新的字符串输出print('\n'.join(new))        

读取文件中的数字并排序输出

已知文本文件 data.txt 中存放了若干数字,每个数字之间以空格分隔。

请编程读取文件中所有的数字,并以从大到小的方式输出到控制台,输出方式为每个数字之间用空格分隔。

"""编程读取 data.txt 文件并以从大到小的方式输出到控制台"""#以只读方式打开文件f = open("data.txt""r", encoding='utf8')#读入文件内容返回字符串存储在s之中s = f.read()#将s进行字符串分割l = s.split(' ')#生成器表达式 int函数取整数 每一个x均来自于l字符串l = [int(x) for x in l]#排序从小到大l.sort()#字符串的反转l.reverse()print(' '.join([str(x) for x in l]))

生成器表达式实例

 s=input('请输入4个数,用英文逗号分隔:') l=s.split(',') l=[int(x) for x in l] print('最大值:',max(l),'最小值:',min(l))

统计文件中关键字出现的次数

综合项目

读取当前目录下的 data.txt 文件,请编程统计关键字中国出现的次数,并输出到控制台。

data.txt 内容

['中国\n''美国\n''中国人民\n''日本\n''中国人']

输出

3
"""读取 data.txt 中的内容统计关键字中国出现的次数,输出至控制台"""text = open('data.txt')s = text.readlines()k = 0text.close()#count()函数:统计在字符串/列表/元组中某个字符出现的次数,可以设置起始位置或结束位置for i in s:    k +=i.count("中国")print(k)

读取文件进行大小写转换

已知文本文件 data.txt 中存放了若干字母,每个字母之间以空格分隔。

请编程读取文件中所有的字母,把其中的大写字母变成小写字母,小写字母变成大写字母,将转化后的字母写到一个新文件result.txt中。

data.txt 内容

a b c D E f z

result.txt 内容

A B C d e F Z
"""读取 data.txt 中空格分隔的字母列表,并将字母进行大小写转化,然后输出至 result.txt"""a = open("data.txt")c = a.readlines()a.close()g = " ".join(c)  # 将列表转化为字符串g = g.swapcase() # 将字符串大小写进行转化print(g)f = open("result.txt","w")  # 写入文件f.write(g)f.close()

对文件内容加密

综合项目

已知文本文件 data.txt 中存放有若干英文字符,每个字符之间以空格分隔,请将该文件中每个英文字母加密后写到一个新文件secret.txt中。

加密规则是:将A变成B,B变成C,…,Y变成Z, Z变成A, a变成b,b变成c,…. y变成z, z变成a,其他字符不变。

data.txt 内容

A B 1 a Y Z

secret.txt

B C 1 b Z A
"""读取 data.txt 中空格分隔的字符列表,并将字符按加密规则进行加密,输出至 secret.txt"""f = open("data.txt","r")content = f.read()newStr = ""for i in content:    temp = ord(i)    if temp in range(65,91):        #字母是z 返回a         if temp == 90:            char1 = chr(temp-25)            newStr += char1        else:            char2 = chr(temp + 1)            newStr += char2     elif temp in range(97,123):        if temp ==122:            char3 = chr(temp-25)            newStr += char3        else:            char4 = chr(temp + 1)            newStr += char4    else:         newStr = newStr + if.close()f2 = open("secret.txt",'w+')f2.write(newStr)f2.seek(0)for line in f2:    print(line)f2.close()

文件拷贝

综合项目

已知文本文件 data.txt 中存放了若干内容。

请编程在当前目录下产生一个相同文件的副本data_cp.txt,实现文件的拷贝功能。

"""编程实现文件拷贝功能"""strNew = ''text = open('data.txt')content = text.readlines()for i in content:    strNew = strNew + itext.close()text2 = open('data_cp.txt','w+')text2.write(strNew)text2.close()

统计文件行数

已知文本文件 data.txt 中每行包含一段英文。

请编程读取文件中的全部内容,统计行数,并按要求输出至控制台。

"""请编程读取文件中的全部内容,统计行数,并按要求输出至控制台。"""s = open('data.txt')context = s.readlines()k = 0for i in context:    k = k+1 s.close()print(k)

统计文件中以P开头的总行数

已知文本文件 data.txt 中每行包含一段英文。

请编程读取文件中的全部内容,统计文件中以大写字母P开头的总行数,并按要求输出至控制台。

"""请编程读取文件中的全部内容,统计以大写P开头的总行数,并按要求输出至控制台。"""s = open('data.txt')context = s.readlines()k = 0for i in context:    if i.startswith('P'):          k = k+1 s.close()print(k)

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-07-04 08:10:44 HTTP/2.0 GET : https://f.mffb.com.cn/a/488559.html
  2. 运行时间 : 0.103830s [ 吞吐率:9.63req/s ] 内存消耗:5,225.20kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=25cc10098c57ea18d4a7aa6e032b9498
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000551s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000806s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.001666s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000369s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000511s ]
  6. SELECT * FROM `set` [ RunTime:0.000228s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000561s ]
  8. SELECT * FROM `article` WHERE `id` = 488559 LIMIT 1 [ RunTime:0.001001s ]
  9. UPDATE `article` SET `lasttime` = 1783123844 WHERE `id` = 488559 [ RunTime:0.021807s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000469s ]
  11. SELECT * FROM `article` WHERE `id` < 488559 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.000544s ]
  12. SELECT * FROM `article` WHERE `id` > 488559 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.001664s ]
  13. SELECT * FROM `article` WHERE `id` < 488559 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.000716s ]
  14. SELECT * FROM `article` WHERE `id` < 488559 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.000979s ]
  15. SELECT * FROM `article` WHERE `id` < 488559 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.000919s ]
0.105417s