• 分支循环相关


    分支:

    1.判断今天是今年第几天

     1 # __author__ = "wyb"
     2 # date: 2018/3/18
     3 # 判断今天是今年第几天
     4 import time
     5 
     6 
     7 def demo(year, month, day):
     8     day_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
     9     if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
    10         day_month[1] = 29
    11     if month == 1:
    12         return day
    13     else:
    14         return sum(day_month[:month - 1]) + day
    15 
    16 
    17 date = time.localtime()
    18 y, m, d = date[:3]
    19 print(demo(y, m, d))

    2.猜年龄

    普通版: 允许用户最多尝试3次,3次都没猜到的话就直接退出,如果猜对了,打印恭喜信息并退出

     1 age = 21
     2 count = 0
     3 while count < 3:
     4     guess_age = int(input("age is: ").strip())
     5     if guess_age == age:
     6         print("恭喜你猜对了!")
     7         break
     8     elif guess_age < age:
     9         print("小了")
    10     elif guess_age > age:
    11         print("大了")
    12     count += 1
    13 else:
    14     print("最多可以尝试3次哦!")

    升级版: 允许用户最多尝试3次,每尝试3次后如果还没猜对就问用户是否继续想玩,回答Y或y继续让其猜3次,回答N或n就退出程序,

    如果猜对了就直接欢迎然后退出

     1 age = 21
     2 count = 0
     3 while count < 3:
     4     guess_age = int(input("age is: ").strip())
     5     if guess_age == age:
     6         print("恭喜你猜对了!")
     7         break
     8     elif guess_age < age:
     9         print("小了")
    10     elif guess_age > age:
    11         print("大了")
    12     count += 1
    13     if count == 3:
    14         choice = input("是否还想继续猜?(输入y继续,输入其他退出): ")
    15         if choice == 'Y' or choice == 'y':
    16             count = 0

    3.判断闰年

    1 # 满足下面两个条件之一的就是闰年:
    2 # (1)能被4整除但不能被100整除 (2)能被400整除

    输入年份判断是否是闰年

     1 # __author__ = "wyb"
     2 # date: 2018/4/14
     3 
     4 year = int(input("Input year: ").strip())
     5 
     6 if year % 4 == 0 and year%100!=0:
     7     print("是闰年")
     8 elif year % 400 == 0:
     9     print("是闰年")
    10 else:
    11     print("不是闰年")

    循环:

    1.任意输入三个英文单词,按字典顺序输出

     1 # __author__ = "wyb"
     2 # date: 2018/3/10
     3 
     4 # 任意输入三个英文单词,按字典顺序输出
     5 
     6 # 方法1:
     7 # s = input('x,y,z=')
     8 # x, y, z = s.split(',')
     9 # if x > y:
    10 #     x, y = y, x
    11 # if x > z:
    12 #     x, z = z, x
    13 # if y > z:
    14 #     y, z = z, y
    15 # print(x, y, z)
    16 
    17 # 方法2:
    18 s = input('x,y,z=')
    19 x, y, z = s.split(',')
    20 x, y, z = sorted([x, y, z])
    21 print(x, y, z)

    2.给一个不超出5位的正整数,判断有几位,并依次打印个位、十位、百位、千位、万位

     1 # __author__ = "wyb"
     2 # date: 2018/2/21
     3 # (1)给一个不超出5位的正整数,判断有几位,并依次打印个位、十位、百位、千位、万位:
     4 # (2)还有没有更好的打印方法? 如果需求是从万位到个位打印呢?
     5 
     6 # (1):
     7 # n = int(input(">>>"))
     8 # print(n)
     9 # i = 0       # 计数
    10 # while n:
    11 #     print(n % 10)
    12 #     n = n//10           # 地板除法(类C语言) -> 5/2 == 2
    13 #     i += 1
    14 #
    15 # print(i)
    16 
    17 # (2): 循环:
    18 # n = int(input(">>>"))
    19 # print(n)
    20 # # 确定位数:
    21 # if n >= 1000:
    22 #     if n >= 10000:
    23 #         num = 5
    24 #     else:
    25 #         num = 4列表
    26 # else:
    27 #     if n >= 100:
    28 #         num = 3
    29 #     elif n >= 10:
    30 #         num = 2
    31 #     else:
    32 #         num = 1
    33 # # 从第一位开始输出:
    34 # print(num)
    35 # pre = 0
    36 # for i in range(num, 0, -1):
    37 #     cur = n//(10**(i-1))
    38 #     print(cur - pre*10)
    39 #     pre = cur
    40 
    41 # (2): 递归函数:
    42 def func(n):
    43     if n//10 == 0:
    44         print(n)
    45         return
    46     else:
    47         func(n//10)
    48         print(n % 10)
    49 
    50 n = int(input(">>>"))
    51 print(n)
    52 func(n)

    3.打印以下菱形:

        *
    ***
    *****
    *******
    *****
    ***
    *
     1 # __author__ = "wyb"
     2 # date: 2018/2/25
     3 
     4 # 打印以下菱形:
     5 #     *
     6 #    ***
     7 #   *****
     8 #  *******
     9 #   *****
    10 #    ***
    11 #     *
    12 
    13 for i in range(-3, 4):
    14     if i < 0:
    15         pre_space = -i
    16     else:
    17         pre_space = i
    18     print(" " * pre_space + '*' * (7 - pre_space * 2))

    4.打印菱形拓展:

       *
    **
    ***
    *******
    ***
    **
    *
     1 # __author__ = "wyb"
     2 # date: 2018/2/25
     3 
     4 # 打印以下图形:
     5 #     *
     6 #    **
     7 #   ***
     8 #  *******
     9 #     ***
    10 #     **
    11 #     *
    12 
    13 for i in range(-3, 4):
    14     if i < 0:
    15         print(" " * (-i) + "*" * (4 + i))
    16     elif i > 0:
    17         print(" " * 3 + "*" * (4 - i))
    18     else:
    19         print("*" * 7)

    5.菲薄纳西数列:(1)打印100以内的菲薄纳西数列(2)求菲薄纳西数列的第101项

     1 # __author__ = "wyb"
     2 # date: 2018/2/21
     3 # (1)打印100以内的菲薄纳西数列
     4 # (2)求菲薄纳西数列的第101项
     5 
     6 # (1):
     7 number_first = 1
     8 number_second = 1
     9 while True:
    10     print(number_first)
    11     if number_second > 100:
    12         break
    13     number_first, number_second = number_second, number_first + number_second
    14 
    15 # (2):
    16 number_first = 1
    17 number_second = 1
    18 count = 1
    19 while True:
    20     if count == 101:
    21         print(number_first)
    22     count += 1
    23     number_first, number_second = number_second, number_first + number_second

    6.质数: (1)判断一个数是否是素数(质数)(2)求1万内的素数

     1 # __author__ = "wyb"
     2 # date: 2018/2/21
     3 # (1)判断一个数是否是素数(质数)
     4 # (2)求1万内的素数
     5 # 关于质数: 一个大于1的自然数只能被1和它本身整除
     6 
     7 # (1):
     8 # number = int(input(">>>"))
     9 # if number == 2:
    10 #     print("is prime")
    11 # else:
    12 #     for i in range(2, number):
    13 #         if number % i == 0:
    14 #             print("not prime")
    15 #             break
    16 #     else:
    17 #         print("is prime")
    18 
    19 
    20 # (2):
    21 import time  # 引入模块
    22 
    23 count = 1  # 计数
    24 print(2)
    25 start_time = time.time()
    26 for number in range(3, 10000, 2):
    27     i = 2
    28     while True:
    29         i += 1
    30         if number % i == 0:
    31             break
    32         if i * i >= number:
    33             print(number)
    34             count += 1
    35             break
    36 end_time = time.time()
    37 t = end_time - start_time
    38 
    39 print("
    ", count)
    40 print(t)

    7.求1到5的阶乘之和: 1! + 2! + 3! + 4! + 5! == 153

     1 # __author__ = "wyb"
     2 # date: 2018/2/21
     3 # 求1到5的阶乘之和: 1! + 2! + 3! + 4列表! + 5!  == 153
     4 
     5 # (1)递归函数求阶乘之和:
     6 # def fact(number):
     7 #     if number <= 1:
     8 #         return 1
     9 #     else:
    10 #         return number * fact(number - 1)
    11 #
    12 # sum_fact = fact(1) + fact(2) + fact(3) + fact(4列表) + fact(5)
    13 # print(sum_fact)
    14 
    15 
    16 # (2)循环求阶乘之和:
    17 num = 1
    18 x = 0
    19 for n in range(1, 6):
    20     num *= n
    21     x += num
    22 print(x)

    8.打印九九乘法表

    1 # __author__ = 'wyb'
    2 # date: 2018/2/22
    3 
    4 for i in range(1, 10):
    5     for j in range(1, i+1):
    6         print(str(j) + " X " + str(i) + " = " + str(i * j) + '	', end=' ')
    7     print()

    9.复利计算

    问题:  假设一年期定期利率为3.25%,计算一下需要经过多少年,一万元的一年定期存款连本带息能翻番?

     1 # __author__ = "wyb"
     2 # date: 2018/4/14
     3 
     4 rate = 0.0325
     5 money = 10000
     6 year = 0
     7 while True:
     8     year += 1
     9     money = money*(1+rate)
    10     print(year)
    11     print(money)
    12     if money >= 20000:
    13         break
    14 
    15 print()
    16 print(year)
    17 print(money)

    10.求100以内的素数和

     1 # __author__ = "wyb"
     2 # date: 2018/4/15
     3 # 求100以内的素数和
     4 
     5 def is_prime(number):
     6     if number == 2:
     7         return True
     8     else:
     9         for i in range(2, number):
    10             if number % i == 0:
    11                 return False
    12         else:
    13             return True
    14 
    15 
    16 res = 0
    17 for n in range(2, 100):
    18     if is_prime(n):
    19         res += n
    20 
    21 print(res)
  • 相关阅读:
    *** 82 删除排序链表中的重复元素II
    83 删除排序链表中的重复元素
    61 旋转链表
    ASP.NET MVC3 ModelState.IsValid为false的问题
    ServletContext
    ServletConfig
    Servlet线程安全
    Servlet的一些细节(2)
    Servlet的一些细节(1)
    Servlet的接口实现类
  • 原文地址:https://www.cnblogs.com/wyb666/p/8796734.html
Copyright © 2020-2023  润新知