分支循环相关
分支:
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.猜年齡
普通版: 允許用戶(hù)最多嘗試3次,3次都沒(méi)猜到的話(huà)就直接退出,如果猜對(duì)了,打印恭喜信息并退出
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("恭喜你猜對(duì)了!") 7 break 8 elif guess_age < age: 9 print("小了") 10 elif guess_age > age: 11 print("大了") 12 count += 1 13 else: 14 print("最多可以嘗試3次哦!")?
升級(jí)版: 允許用戶(hù)最多嘗試3次,每嘗試3次后如果還沒(méi)猜對(duì)就問(wèn)用戶(hù)是否繼續(xù)想玩,回答Y或y繼續(xù)讓其猜3次,回答N或n就退出程序,
如果猜對(duì)了就直接歡迎然后退出
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("恭喜你猜對(duì)了!") 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("是否還想繼續(xù)猜?(輸入y繼續(xù),輸入其他退出): ") 15 if choice == 'Y' or choice == 'y': 16 count = 0?
3.判斷閏年
1 # 滿(mǎn)足下面兩個(gè)條件之一的就是閏年: 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("不是閏年")?
?
?
循環(huán):
1.任意輸入三個(gè)英文單詞,按字典順序輸出
1 # __author__ = "wyb" 2 # date: 2018/3/10 3 4 # 任意輸入三個(gè)英文單詞,按字典順序輸出 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.給一個(gè)不超出5位的正整數(shù),判斷有幾位,并依次打印個(gè)位、十位、百位、千位、萬(wàn)位
1 # __author__ = "wyb" 2 # date: 2018/2/21 3 # (1)給一個(gè)不超出5位的正整數(shù),判斷有幾位,并依次打印個(gè)位、十位、百位、千位、萬(wàn)位: 4 # (2)還有沒(méi)有更好的打印方法? 如果需求是從萬(wàn)位到個(gè)位打印呢? 5 6 # (1): 7 # n = int(input(">>>")) 8 # print(n) 9 # i = 0 # 計(jì)數(shù) 10 # while n: 11 # print(n % 10) 12 # n = n//10 # 地板除法(類(lèi)C語(yǔ)言) -> 5/2 == 2 13 # i += 1 14 # 15 # print(i) 16 17 # (2): 循環(huán): 18 # n = int(input(">>>")) 19 # print(n) 20 # # 確定位數(shù): 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 # # 從第一位開(kāi)始輸出: 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): 遞歸函數(shù): 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.菲薄納西數(shù)列:(1)打印100以?xún)?nèi)的菲薄納西數(shù)列(2)求菲薄納西數(shù)列的第101項(xiàng)
1 # __author__ = "wyb" 2 # date: 2018/2/21 3 # (1)打印100以?xún)?nèi)的菲薄納西數(shù)列 4 # (2)求菲薄納西數(shù)列的第101項(xiàng) 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.質(zhì)數(shù):?(1)判斷一個(gè)數(shù)是否是素?cái)?shù)(質(zhì)數(shù))(2)求1萬(wàn)內(nèi)的素?cái)?shù)
1 # __author__ = "wyb" 2 # date: 2018/2/21 3 # (1)判斷一個(gè)數(shù)是否是素?cái)?shù)(質(zhì)數(shù)) 4 # (2)求1萬(wàn)內(nèi)的素?cái)?shù) 5 # 關(guān)于質(zhì)數(shù): 一個(gè)大于1的自然數(shù)只能被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 # 計(jì)數(shù) 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)?
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)遞歸函數(shù)求階乘之和: 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)循環(huán)求階乘之和: 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) + '\t', end=' ') 7 print()?
9.復(fù)利計(jì)算
問(wèn)題:? 假設(shè)一年期定期利率為3.25%,計(jì)算一下需要經(jīng)過(guò)多少年,一萬(wàn)元的一年定期存款連本帶息能翻番?
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以?xún)?nèi)的素?cái)?shù)和
1 # __author__ = "wyb" 2 # date: 2018/4/15 3 # 求100以?xún)?nèi)的素?cái)?shù)和 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)?
轉(zhuǎn)載于:https://www.cnblogs.com/wyb666/p/8796734.html
總結(jié)
- 上一篇: BZOJ4568:[SCOI2016]幸
- 下一篇: MFC中修改程序图标