python练习题1

#质数判断
while True:
    try:
        x = int(input("请输入一个数字"))
        for i in range(2,x):
            if x % i == 0:
                print("{0}不是质数".format(x))
                break
        else:
            print("%d是质数"%x)
        break
    except ValueError:
        print("您输入的不是数字")

#阶乘
num = int(input("请输入一个数字"))
j =  1
if num < 0:
    print("请输入正数")
elif num == 0:
    print("0!等于1")
else:
    for i in range(1,num + 1):
        j = j * i
    print("{0}的阶乘等于{1}".format(num,j))


#九九乘法表

for i in range(0,10):
    for j in range(0,i):
        #\t是制表符,print是默认换行的,end是指定末尾输出,实现不换行
        print("{0}x{1}={2}\t".format(i,j,i*j),end='')
    #输出一行之后换行
    print()

#斐波那契数列
num = int(input("请输入几项"))
aa = [0,1]
for i in range(0,num-2):
    k = aa[i] + aa[i+1]
    aa.append(k)
print(aa)

#阿姆斯特朗数,判断某数是不是阿姆斯特朗数
sum = 0
temp = num
while temp > 0:
    sum = sum + (temp % 10) ** len(str(num))
    temp = temp // 10
if sum == num:
    print("%d是阿姆斯特朗数"%num)
else:
    print("{0}不是阿姆斯特朗数".format(num))


#阿姆斯特朗数
bb = []
for i in range(0,10010):
    length = len(str(i))
    #//取整除,**优先级最高
    if length == 4 and (i//1000)**4 + (i%1000//100)**4 + (i%100//10)**4 + (i%10)**4 == i:
        bb.append(i)
    elif length == 3 and pow((i//100),3) + pow((i%100//10),3) + pow((i%10),3) == i:
        bb.append(i)
    elif length == 2 and pow((i/10),2) + pow((i%10),2) == i:
        bb.append(i)
    elif length == 1 and i**1 == i:
        bb.append(i)
    else:
        pass
print(bb)

#输出0到某数之内所有阿姆斯特朗数
aa = []
num_aa = int(input("请输入数字"))
for i in range(0,num_aa+1):
    length =len(str(i))
    sum = 0
    temp = i
    while temp > 0:
        sum = sum + (temp % 10) ** length
        temp = temp // 10
    if sum == i:
        aa.append(i)
    else:
        pass
print(aa)

执行结果:
请输入一个数字1
1是质数
请输入一个数字1
1的阶乘等于1

1x0=0
2x0=0 2x1=2
3x0=0 3x1=3 3x2=6
4x0=0 4x1=4 4x2=8 4x3=12
5x0=0 5x1=5 5x2=10 5x3=15 5x4=20
6x0=0 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30
7x0=0 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42
8x0=0 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56
9x0=0 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72
请输入几项153
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075, 573147844013817084101, 927372692193078999176, 1500520536206896083277, 2427893228399975082453, 3928413764606871165730, 6356306993006846248183, 10284720757613717413913, 16641027750620563662096, 26925748508234281076009, 43566776258854844738105, 70492524767089125814114, 114059301025943970552219, 184551825793033096366333, 298611126818977066918552, 483162952612010163284885, 781774079430987230203437, 1264937032042997393488322, 2046711111473984623691759, 3311648143516982017180081, 5358359254990966640871840, 8670007398507948658051921, 14028366653498915298923761, 22698374052006863956975682, 36726740705505779255899443, 59425114757512643212875125, 96151855463018422468774568, 155576970220531065681649693, 251728825683549488150424261, 407305795904080553832073954, 659034621587630041982498215, 1066340417491710595814572169, 1725375039079340637797070384, 2791715456571051233611642553, 4517090495650391871408712937, 7308805952221443105020355490, 11825896447871834976429068427, 19134702400093278081449423917, 30960598847965113057878492344, 50095301248058391139327916261, 81055900096023504197206408605, 131151201344081895336534324866, 212207101440105399533740733471, 343358302784187294870275058337, 555565404224292694404015791808, 898923707008479989274290850145, 1454489111232772683678306641953, 2353412818241252672952597492098, 3807901929474025356630904134051, 6161314747715278029583501626149, 9969216677189303386214405760200, 16130531424904581415797907386349, 26099748102093884802012313146549]
153是阿姆斯特朗数
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]
请输入数字10000
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]

Process finished with exit code 0

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页