python练习题

python练习题

1、输入几个整数,按大到小输出

>>>
a = input()
xlist = a.split(',')
xlist = [int(xlist[i]) for i in range(len(xlist))]
for j in range(len(xlist)):
    for h in range(len(xlist)):
        if h<=j:
            h=j
        h += 1
        if h < len(xlist):
            if xlist[j] < xlist[h]:
                c = xlist[j]
                xlist[j] = xlist[h]
                xlist[h] = c
print(xlist)
C:\ProgramData\Anaconda3\python.exe D:/python/housework/a.py
23,1,4,5
[23, 5, 4, 1]

Process finished with exit code 0

2、判断这一天是一年中多少天

import datetime
import time
dtstr=input('enter the datetime(20151231):')
dt=datetime.datetime.strptime(dtstr,'%Y%m%d')
print(dt)
another_dtstr=dtstr[:4]+'0101'
another_dt=datetime.datetime.strptime(another_dtstr,"%Y%m%d")
print(another_dt)
print(int((dt-another_dt).days)+1)
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
enter the datetime(20151231):20200201
2020-02-01 00:00:00
2020-01-01 00:00:00
32

Process finished with exit code 0

3、古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少

def robot(mouth):#mouth为月数
    if mouth==0:
        return 1
    elif mouth==1:
        return 2
    else:
        count=robot(mouth-1)+robot(mouth-2)
        return count

print(robot(3))#递归问题,count为总数
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
5
Process finished with exit code 0

4、求100到150之间素数

for i in range(100,151):
    if (i%2!=0) and (i%3!=0) and (i%5!=0):
        print(i,end=',')
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
101,103,107,109,113,119,121,127,131,133,137,139,143,149,
Process finished with exit code 0

5、输入一个三位数,判断该数是否为水仙花数(水仙花数为各位数立方和等于该数,如153是‘水仙花数’,153=1的三次方+5的三次方+3的三次方)

totel=int(input('输入三位数:'))
million=int(totel/100)
totel2=totel%100
ten=int(totel2/10)
one=totel2%10
test=million**3+ten**3+one**3
if test==totel:
    print('%d是水仙花数'% totel)
else:
    print('no')
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
输入三位数:153
153是水仙花数

Process finished with exit code 0

6、将一个正整数分解质因数。如90,打印出90=2*3*3*5

a=int(input('请输入正整数:'))
while (a!=1):
    for i in range(2,a+1):
        if (a%i)==0:
            a/=i
            if(a==i):
                print('%d'%i,end=',')
            else:
                print('%d'%i,end=',')
            a=int(a)
            break
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
请输入正整数:32
2,2,2,2,2,
Process finished with exit code 0

7、输出9*9口诀

for i in range(1,10):
    for j in range(1,10):
        a=i*j
        print('%d*%d=%d'%(i,j,a))

1*1=1
、、、
9*7=63
9*8=72
9*9=81

Process finished with exit code 0

8、利用条件运算符的嵌套:90<=学习成绩<=100用aaa表示,70<=学习成绩<=89用bbb表示,60<=学习成绩<=69用ccc表示

a=int(input('请输入分数:'))
if a>=90 and a<=100:
    print('aaa')
elif a>=70 and a<=89:
    print('bbb')
elif a>=60 and a<=69:
    print('ccc')
else:
    print('error')

C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
请输入分数:89
bbb

Process finished with exit code 0

9、输入一段字符串,求出所包含数字、字母、空格和其他字符各个总数

a=input('请输入字符串:')
a=list(a)
alphabet=0;number=0;null=0;other=0

for i in range(0,len(a)):
    if (ord(a[i])>=97 and ord(a[i])<=122) :
        alphabet+=1
    elif (ord(a[i])>=65 and ord(a[i])<=90):
        alphabet+=1
    elif ord(a[i])>=48 and ord(a[i])<57:
        number+=1
    elif ord(a[i])==32:
        null+=1
    else:
        other+=1
print('alphabet=%d,number=%d,null=%d,other=%d'%(alphabet,number,null,other))
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
请输入字符串:www.ban soso beautiful666!!
alphabet=19,number=3,null=2,other=3

Process finished with exit code 0

10、求s=a+aa+aaa+a…a的值,其中a是一个数字,例如2+22+222+2222(此时共有4个数相加),几个数相加由键盘控制’

a=int(input('input the basis number:'))
b=int(input('input the longest length of number:'))
c=0
for i in range(b+1):
    c+=(b-i)*a*10**i
print('一共为%d'%c)
C:\ProgramData\Anaconda3\python.exe D:/python/housework/a.py
input the basis number:2
input the longest length of number:3
一共为246

Process finished with exit code 0

11、一个数恰好等于它的因子之和,这个数称为‘完数。例如:6=1+2+3。找出1000以内的所有完数’

for i in range(1,1001):
    two=0;three=0;five=0;basis=i
    while 1:
        if i%2==0:
            two+=1
            i=i/2
        elif i%3==0:
            three+=1
            i=i/3
        elif i%5==0:
            five+=1
            i=i/5
        else:
            other=i
            break
    other1=0
    if other != 1:
        other1=other
    totel=1+two*2+three*3+five*5+other1
    if totel==basis and basis!=1:
        print('%d'%basis)
C:\ProgramData\Anaconda3\python.exe D:/python/housework/a.py
6

Process finished with exit code 0

12、一球从100米高度自由下落,每次落地反弹回高度为原高度的一半,再下落,求它在第n次落地共经过路程为多少,和第n次反弹后有多高

a=100;c=a
numer=int(input('请输入第几次落地:'))
if numer>=2:
    for i in range(2,numer+1):
        c=c*(1/2)
        a=a+c*2
    print('共经过路程为%f,第%d次反弹高度为%f'%(a,numer,c*(1/2)))
elif numer==1:
    print('共经过路程为100米,第一次弹起后为50米')
else:
    print('error')
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
请输入第几次落地:10
共经过路程为299.609375,第10次反弹高度为0.097656

Process finished with exit code 0

13、猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾又多吃一个;第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天都吃了前一天剩下的一半零一个。到了第十天早上想再吃时,见只剩下一个。问第一次共摘了多少

n=1
for i in range(9,0,-1):
    n=(n+1)<<1
print(n)
C:\ProgramData\Anaconda3\python.exe D:/python/housework/a.py
1534

Process finished with exit code 0

14、打印出下列图案

   *
  ***
 *****
*******
 *****
  ***
   *
for i in range(1,8,2):
 print(' '*int((4-(1+i)/2))+'*'*i)
for j in range(5,0,-2):
 print(' '*(3-int(j/2))+'*'*j)
C:\ProgramData\Anaconda3\python.exe D:/python/housework/a.py
   *
  ***
 *****
*******
 *****
  ***
   *

Process finished with exit code 0

15、两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x、y、z三人。抽签决定:a说他不和x比,c说他不和x,z比,请编程序找出三队赛手名单

for i in range(ord('x'),ord('z')+1):
    for j in range(ord('x'),ord('z')+1):
        if i!=j:
            for k in range(ord('x'),ord('z')+1):
                if (i!=k) and (j!=k):
                    if (i!=ord('x')) and (k!=ord('x')) and (k!=ord('z')):
                        print('order is a--%s\t b--%s\t c--%s'%
                              (chr(i),chr(j),chr(k)))
order is a--z	 b--x	 c--y

16、有一串数列:2/1,3/2,5/3,8/5,13/8…求这数列前20项之和

a=2;b=1;i=1;d=2
j=int(input('请输入前n项整整:'))
while j>1 and i<j:
    i+=1
    f=a
    a=a+b
    b=f
    c=a/b
    d=d+c
if j>=1:
    print(d)
else:
    print('error')
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
请输入前n项整整:20
32.66026079864164

Process finished with exit code 0

17、输出1!+2!+…+20!之和

totel2=0
for i in range(1,21):
    totel1=1
    for i in range(1,i+1):
        totel1=totel1*i
    totel2=totel2+totel1
print(totel2)
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
2561327494111820313

Process finished with exit code 0

18、使用递归函数,输入一串字符串后按相反的顺序输出

a='abcde'
b=len(a)-1
def cir(b):
    print(a[b])
    
    b-=1
    cir(b)
C:\ProgramData\Anaconda3\python.exe D:/python/housework/test.py
please string:applo
o
l
p
p
a

Process finished with exit code 0

19、对10个数进行排序

a=[]
for i in range(10):
    a.append(int(input()))
    
for i in range(10):
    for j in range(i,10):
        if a[i]>a[j]:
            a[i],a[j]=a[j],a[i]
            
print(a)
[1,4,6,7,8,9,13,21,323,4565]

www.ban白嫖不易,三连ilil

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值