Python演習2

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.着信リストの最大および2番目に大きい要素の値を返す関数を設計します。

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. Yang Huiの三角形を印刷します。

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