Python练习题二

1、实现计算求最大公约数和最小公倍数的函数。

def gcd_lcm(a,b):
    a1 = a
    b1 = b
    while True:
        r = a % b
        if r == 0:
            print("最大公约数为%d" % b)
            print("最小公倍数为%d" % (a1* b1 / b))
            break
        else:
            a = b
            b = r
    return
c=int(input())
d=int(input())
gcd_lcm(c,d)

2、实现判断一个数是不是回文数的函数。

def num(x):
    x1=x
    y=0
    while x>0:
        y=y*10+x%10
        x=x//10
    if x1==y:
        print("%d是回文数"%x1)
    else:
        print("%d不是回文数"%x1)
a=int(input())
num(a)

3、实现判断一个数是不是素数的函数。

import math
def prime(x):
    sx=int(math.sqrt(x))+1
    a=True
    for i in range(2,sx):
        if x%i==0:
            a=False
            break
    if a==True and x!=1:
        print("%d是素数"%x)
    else:
        print("%d不是素数"%x)
b=int(input())
prime(b)

4、写一个程序判断输入的正整数是不是回文素数。

import math
def prime(x):
    sx=int(math.sqrt(x))+1
    a=True
    for i in range(2,sx):
        if x%i==0:
            a=False
            break
    if a==False or x==1:
        a=False
    return a
def num(x):
    x1=x
    y=0
    while x>0:
        y=y*10+x%10
        x=x//10
    if x1==y:
        b=True
    else:
        b=False
    return b
c=int(input())
if prime(c) and num(c):
    print("%d是回文素数"%c)
else:
    print("%d不是回文素数"%c)

5、在屏幕上显示跑马灯文字:Hello,World

import time
def light():
    sentence="Hello,World"
    while True:
        print(sentence)
        time.sleep(0.3)
        sentence=sentence[1:]+sentence[0]
light()

6、设计一个函数产生指定长度的验证码,验证码由大小写字母和数字构成。

import random
def check(x):
    ch='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    l=len(ch)
    b=''
    for _ in range(x):
        a=random.randint(0,l-1)
        b+=ch[a]
    print("验证码为:",b)
n=int(input())
check(n)

7、设计一个函数返回传入的列表中最大和第二大的元素的值。

def maxx(x):
    x.sort()
    return x[-1],x[-2]
a=[0,1,2,3,4,5,6]
b,c=maxx(a)
print(b,c)

8、计算指定的年月日是这一年的第几天。

import calendar
year=int(input())
month=int(input())
day=int(input())
if calendar.isleap(year)==True:
    ms=[31,29,31,30,31,30,31,31,30,31,30,31]
else:
    ms=[31,28,31,30,31,30,31,31,30,31,30,31]
da=0
for i in range(month-1):
    da+=ms[i]
da+=day
print("%d年%d月%d日是这一年的第%d天"%(year,month,day,da))

9、打印杨辉三角。

def triangle(x):
    t=[[]]*x
    for r in range(x):
        t[r]=[None]*(r+1)
        for c in range(r+1):
            if c==0 or c==r:
                t[r][c]=1
            else:
                t[r][c]=t[r-1][c-1]+t[r-1][c]
            print(t[r][c],end='\t')
        print()
n=int(input())
triangle(n)

10、定义一个类描述平面上的点并提供移动点和计算到另一个点距离的方法。

import math
class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def move1(self,x1,y1):
        self.x+=x1
        self.y+=y1
    def move2(self,x2,y2):
        self.x=x2
        self.y=y2
    def distance(self,point2):
        dx=self.x-point2.x
        dy=self.y-point2.y
        return math.sqrt(dx**2+dy**2)
a=int(input())
b=int(input())
p1=Point(a,b)
c=int(input())
d=int(input())
p2=Point(c,d)
a=int(input())
b=int(input())
p1.move1(a,b)
c=int(input())
d=int(input())
p2.move2(c,d)
print(p1.distance(p2))

11、定义一个类描述数字时钟。

import time
class Clock:
    def __init__(self,hour,minute,second):
        self.hour=hour
        self.minute=minute
        self.second=second
    def move(self):
        self.second+=1
        if self.second==60:
            self.second=0
            self.minute+=1
            if self.minute==60:
                self.minute=0
                self.hour+=1
                if self.hour==24:
                    self.hour=0
    def display(self):
        print("%02d:%02d:%02d"%(self.hour,self.minute,self.second))
h=int(input())
m=int(input())
s=int(input())
c=Clock(h,m,s)
while True:
    c.display()
    time.sleep(1)
    c.move()
发布了48 篇原创文章 · 获赞 25 · 访问量 2453

猜你喜欢

转载自blog.csdn.net/qq_43628959/article/details/100939367