欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python十个实例(二)

程序员文章站 2024-03-17 16:08:52
...

0x00 判断奇偶

while True:
    try:
        num = int(input("请输入一个整数:"))
    except ValueError:
        print("输入的不是整数!")
        continue

    if num%2 == 0:
        print("这是一个偶数!")
    else:
        print("这是一个奇数!")
    break

0x01 判断闰年

year = int(input("请输入一个年份:"))

if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
    print("%d是闰年" % year)
else:
    print("%d不是闰年" % year)

0x02 比较大小

while True:
    try:
        N = int(input("请输入需要比较的数字个数:"))
        print("请输入需要对比的数字:")
        num = []

        for i in range(1,N+1):
            temp = int(input("输入第%d个数字:" % i))
            num.append(temp)

        print("您输入的数字为:", num)
        print("最大值为:", max(num))
        break
    except ValueError:
        print("输入有误!")

0x03 判断质数

while True:
    try:
        num = int(input("请输入一个数字: "))
         
        if num > 1:
           for i in range(2,num):
               if (num % i) == 0:
                   print(num,"不是质数")
                   print(i,"乘",num//i,"等于",num)
                   break
           else:
               print(num,"是质数")      
        else:
           print(num,"不是质数")
        break
    except ValueError:
        print("输入有误!")

0x04 输出指定范围内的质数

while True:
    try:
        min = int(input("输入区间最小值:"))
        max = int(input("输入区间最大值:"))
        print(min,"和",max,"之间的质数有:")

        for n in range(min,max + 1):
            if n > 1:
                for i in range(2,n):
                    if(n % i) == 0:
                        break
                else:
                    print(n)
        break
    except ValueError:
        print("输入有误!")

0x05 输出阶乘

while True:
    try:
        n = int(input("请输入一个数字:"))
        factorial = 1

        if n < 0:
            print("Sorry,负数没有阶乘!")
        elif n == 0:
            print("0的阶乘为1")
        else:
            for i in range(1,n+1):
                factorial = factorial * i
            print("%d的阶乘为%d" % (n,factorial))
        break
    except ValueError:
        print("输入有误!")

0x06 九九乘法表

for i in range(1,10):
    for j in range(1,i+1):
        if i == j:
            print("{1}*{0}={2}".format(i, j, i*j))
        else:
            print("{1}*{0}={2}".format(i, j, i*j),end='\t')

0x07 斐波那契数列

def fib(n):
    a,b = 0,1
    for i in range(n+1):
        a,b = b,a+b
    return a

while True:
    try:        
        N = int(input("请输入要生成数列的项数:"))
        if N <= 0:
            print("请输入正整数!")            
        else:
            for i in range(N):
                print(fib(i),end=' ')
        break
    except ValueError:
        print("输入有误!")

0x08 阿姆斯特朗数

while True:
    try:        
        num = int(input("请输入一个正整数:"))
        if num <= 0:
            print("输入有误!")
            continue

        sum = 0

        n = len(str(num))

        temp = num
        while temp > 0:
            digit = temp % 10
            sum += digit ** n
            temp //=10

        if num == sum:
            print("%d是阿姆斯特朗数" % num)
        else:
            print("%d不是阿姆斯特朗数" % num)
        break

    except ValueError:
        print("输入有误!")

0x09 进制转换

while True:
    try:
        temp = int(input("请输入数字: "))

        print("十进制数为:",temp)
        print("二进制数为:",bin(temp))
        print("八进制数为:",oct(temp))
        print("十六进制数为:",hex(temp))
        break
    except ValueError:
        print("输入有误!")
相关标签: Python python