码迷,mamicode.com
首页 > 其他好文 > 详细

分支循环相关

时间:2018-04-13 20:38:45      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:引入   first   while   字典顺序   需求   个数   输出   NPU   false   

分支:

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.输入分数,输出分数对应等级

 1 # __author__ = "wyb"
 2 # date: 2018/2/21
 3 # 判断学生成绩,成绩等级A-E,90分以上为A,80-89为B,70-79为C,60-69为D,60以下为E
 4 
 5 score = int(input(">>>"))
 6 if score > 100:
 7     print("请输入正确的分数(0-100)")
 8 elif score >= 90:
 9     print("A")
10 elif score >= 80:
11     print("B")
12 elif score >= 70:
13     print("C")
14 elif score >= 60:
15     print("D")
16 elif score >= 0:
17     print("E")
18 else:
19     print("请输入正确的分数(0-100)")

 

3.猜年龄

普通版: 允许用户最多尝试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 flag = True
 4 while flag:
 5     guess_age = int(input("age is: ").strip())
 6     if guess_age == age:
 7         print("恭喜你猜对了!")
 8         break
 9     elif guess_age < age:
10         print("小了")
11     elif guess_age > age:
12         print("大了")
13     count += 1
14     if count == 3:
15         choice = input("是否还想继续猜?(输入y继续,输入其他退出): ")
16         if choice == Y or choice == y:
17             flag = True
18             count = 0
19         else:
20             flag = False

 

 

循环:

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.打印边长为N的正方形

 1 # __author__ = "wyb"
 2 # date: 2018/2/21
 3 # 打印边长为N的正方形:
 4 
 5 # 长和宽上的符号个数相等但是由于每一行之间存在距离,所以看起来不像正方形
 6 # N = int(input(">>>"))
 7 # for i in range(0, N):
 8 #     if i == 0 or i == N-1:
 9 #         for j in range(0, N):
10 #             print("*", end="")
11 #     else:
12 #         for j in range(0, N):
13 #             if j == 0 or j == N-1:
14 #                 print("*", end="")
15 #             else:
16 #                 print(" ", end="")
17 #     # 换行:
18 #     print("")
19 
20 # 调整之后:
21 N = int(input(">>>"))
22 for i in range(0, N):
23     if i == 0 or i == N-1:
24         for j in range(0, 2*N):
25             print("*", end="")
26     else:
27         for j in range(0, 2*N):
28             if j == 0 or j == 2*N-1:
29                 print("*", end="")
30             else:
31                 print(" ", end="")
32     # 换行:
33     print("")

 

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         pre_space = -i
16     else:
17         pre_space = i
18     print(" " * pre_space + * * (7 - pre_space * 2))

 

5.打印菱形拓展:

   *
**
***
*******
***
**
*
 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)

 

6.菲薄纳西数列:(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

 

7.质数: (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("\n", count)
40 print(t)

 

8.求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)

 

9.打印九九乘法表

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) + \t, end= )
7     print()

 

分支循环相关

标签:引入   first   while   字典顺序   需求   个数   输出   NPU   false   

原文地址:https://www.cnblogs.com/wyb666/p/8796734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!