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

第五讲练习

程序员文章站 2022-06-16 18:10:29
3.求1000以内所有的水仙花数def narcissus_number(): number=[] for i in range(1000): hundred = int(i / 100); decades = int((i - hundred*100) / 10) unit = i%10 if(hundred != .0): if(i == hundred**3 + decades**3 + un...

3.求1000以内所有的水仙花数

def narcissus_number():
    number=[]
    for i in range(1000):
        hundred = int(i / 100);
        decades =  int((i - hundred*100) / 10)
        unit = i%10
        if(hundred != .0):
            if(i == hundred**3 + decades**3  + unit**3):
                number.append(i)
    print(number)

if __name__ == '__main__':
    narcissus_number()

4.获取用户输入的任意数,判断其是否是质数?

def is_prime():
    number = int (input("here"))
    print (number)
    square = int(int(number) ** 0.5)
    # 只要数字能被2~ 根号(数字)之间被整除,就不是质数
    for i in range(2,square+2):
        if (number % i == 0):
            break;
    if(i>square):
        print("is prime")
    else:
        print("not")
        
if __name__ == '__main__':
    is_prime()

5. 猜拳游戏:

• 出拳 玩家:手动输入 电脑:随机输入
• 判断输赢: 玩家获胜 电脑获胜 平局

def finger_guessing_game():

    while True:
        fist = int( input('1:石头,2:剪刀,3:布'))
        if(fist < 1 or fist > 3 ):
            print("error")
            continue

        cpu =int( random.randint(1, 3))
        if(fist == 1):
            if(fist == cpu):
                print("你输了平局")
            elif(cpu == 2):
                print("你赢了")
            elif (cpu == 3):
                print("你输了")
        elif (fist == 2):
            if (fist == cpu):
                print("你输了平局")
            elif (cpu == 1):
                print("你输了")
            elif (cpu == 3):
                print("你赢了")
        elif (fist == 3):
            if (fist == cpu):
                print("你输了平局")
            elif (cpu == 1):
                print("你赢了")
            elif (cpu == 2):
                print("你输了")

if __name__ == '__main__':
    finger_guessing_game()

本文地址:https://blog.csdn.net/KHaruki2019/article/details/109852678