python 构造程序逻辑

  1. 寻找“水仙花数”。
    找出100~999之间的所有水仙花数
    水仙花数是各位立方和等于这个数本身的数
    for num in range(100, 1000):
        low = num % 10
        mid = num // 10 % 10
        high = num // 100
        if num == low ** 3 + mid ** 3 + high ** 3:
            print(num)

     

  2. 寻找“完美数”。
    import time
    import math
    
    for num in range(1, 10000):
        sum = 0
        for factor in range(1, int(math.sqrt(num)) + 1):
            if num % factor == 0:
                sum += factor
                if factor > 1 and num / factor != factor:
                    sum += num / factor
        if sum == num:
            print(num)

     

  3. “百钱百鸡”问题。
    for x in range(0, 20):
        for y in range(0, 33):
            z = 100 - x - y
            if 5 * x + 3 * y + z / 3 == 100:
                print('公鸡: %d只, 母鸡: %d只, 小鸡: %d只' % (x, y, z))

     

  4. 生成“斐波拉切数列”。
    a = 0
    b = 1
    for _ in range(20):
        a, b = b, a + b
        print(a, end=' ')

     

  5. Craps赌博游戏。
    from random import randint
    
    money = 1000
    while money > 0:
        print('你的总资产为:', money)
        needs_go_on = False
        while True:
            debt = int(input('请下注: '))
            if debt > 0 and debt <= money:
                break
        first = randint(1, 6) + randint(1, 6)
        print('玩家摇出了%d点' % first)
        if first == 7 or first == 11:
            print('玩家胜!')
            money += debt
        elif first == 2 or first == 3 or first == 12:
            print('庄家胜!')
            money -= debt
        else:
            needs_go_on = True
    
        while needs_go_on:
            current = randint(1, 6) + randint(1, 6)
            print('玩家摇出了%d点' % current)
            if current == 7:
                print('庄家胜')
                money -= debt
                needs_go_on = False
            elif current == first:
                print('玩家胜')
                money += debt
                needs_go_on = False
    
    print('你破产了, 游戏结束!')
    """
      判断输入的正整数是不是回文数
      回文数是指将一个正整数从左往右排列和从右往左排列值一样的数
       
       
       
       
      """
       
      num = int(input('请输入一个正整数: '))
      temp = num
      num2 = 0
      while temp > 0:
      num2 *= 10
      num2 += temp % 10
      temp //= 10
      if num == num2:
      print('%d是回文数' % num)
      else:
      print('%d不是回文数' % num)

你可能感兴趣的:(Python学习)