Springboot黑馬點評(3)——優惠券秒殺 【還剩Redisson的最後兩節沒測試 後續補上】 另外,後期單獨整理一份關於分散式鎖筆記 1 優惠券秒殺實現 1.1 用戶-優惠券訂單設計 1.1.1 全局ID生成器 使用資料庫自增ID作為訂單ID存在問題 1.1.2 考慮全局唯一ID生成邏輯 時 ...
判斷和迴圈
1 判斷
1.1 判斷的三種形式
1.2 判斷的嵌套
2 迴圈
2.1 while迴圈
2.2 for迴圈
3 作業
3.1 輸入年齡進行判斷
3.2 列印偶數
3.3 猜年齡游戲
3.4 9*9乘法表
3.5 金字塔的列印
1 判斷
1.1 判斷的三種形式
1.if
2.if……else
3.if……elif……else
#直接if的語句
real_name ='xiaocao'
name = input('please enter the name:')
if name == real_name:
print('congratulations!')
please enter the name:xiaocao
congratulations!
#if……else的語句
real_name ='xiaocao'
name = input('please enter the name:')
if name == real_name:
print('congratulations!')
else:
print('what a pity! Guess wrong')
please enter the name:xiaofang
what a pity! Guess wrong
# if……elif……else
real_age = 18
age = int(input("please enter the age"))
if age <18:
print("sorry,guess younger")
elif age >18:
print("sorry,guess older")
else:
print('congratulations!')
please enter the age25
sorry,guess older
1.2 判斷語句的嵌套
說白了就是一個if語句中又嵌套了一個if語句
# if……elif……else
real_age = 18
age = int(input("please enter the age"))
if age != 18:
print("sorry,guess wrong")
if age >18:
print("sorry,guess older")
else:
print("sorry,guess younger")
else:
print('congratulations!')
please enter the age25
sorry,guess wrong
sorry,guess older
2 迴圈
2.1 while迴圈
在迴圈中有兩個比較重要的函數:
break和continue
break就是直接回到開始的地方
continue就是跳出這一步,後面繼續
#while的迴圈好處就是可以直接將所需要的判斷條件作為一個真實值增加break進行判斷
real_age = 18
while True:
age = int(input("please enter the age"))
if age == 18:
print('congratulations!')
break
else:
print("sorry,guess wrong")
if age >18:
print("sorry,guess older")
else:
print("sorry,guess younger")
please enter the age23
sorry,guess wrong
sorry,guess older
please enter the age15
sorry,guess wrong
sorry,guess younger
please enter the age18
congratulations!
#迴圈中的continue的用法
num = 0
while num <=15:
if num == 10:
num +=1
continue
else:
print(num)
num+=1
0
1
2
3
4
5
6
7
8
9
11
12
13
14
15
2.1.1 while的嵌套
就是在一個while語句裡面包含了另一個的while語句
# 註意,這裡有幾個迴圈終止的時候就需要幾個break
real_age = 18
while True:
age = int(input("please enter the age"))
if age == 18:
print('congratulations!')
while True:
prize_dict = {0:'toy_car',1:'doll',2:'puzzle'}
print(f'please choose one of these gifts: {prize_dict}')
prize = int(input('please enter the num:'))
if prize == 1:
print(f'Congratulations, you have received this gift,the gift is {prize_dict[1]}')
break
else:
print("Sorry, we don't have this gift.Please reselect")
break
else:
print("sorry,guess wrong")
if age >18:
print("sorry,guess older")
else:
print("sorry,guess younger")
please enter the age23
sorry,guess wrong
sorry,guess older
please enter the age15
sorry,guess wrong
sorry,guess younger
please enter the age18
congratulations!
please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}
please enter the num:2
Sorry, we don't have this gift.Please reselect
please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}
please enter the num:1
Congratulations, you have received this gift,the gift is doll
2.2 for迴圈
其實和while差不多,好處就是他不會溢出,知道位置便可以列印了
這個迴圈函數可以做一些效果出來
# 數據實例引用水導的知識點,我只是在慢慢學習啦
game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
for i in game_list:
print(i)
xiaoxiaokan
kaixinxiaoxiaole
tiaoyitiao
chaojimali
hundouluo
zhizhuzhipai
saolei
renzheshengui
2.2.1 for+break的方法
game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
for i in game_list:
if i == 'tiaoyitiao':
break
print(i)
xiaoxiaokan
kaixinxiaoxiaole
2.2.2 for+continue
game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
for i in game_list:
if i == 'tiaoyitiao':
continue
print(i)
xiaoxiaokan
kaixinxiaoxiaole
chaojimali
hundouluo
zhizhuzhipai
saolei
renzheshengui
2.2.3 for迴圈的嵌套
這個真的代碼裡面最常用,但是底層邏輯相對而言不是那麼難
for i in range(3):
for j in range(3):
if i*j !=0:
print(i*j)
1
2
2
4
2.2.4 for+else
for i in range(3):
for j in range(3):
if i*j !=0:
print(i*j)
else:
print('print finish')
1
2
2
4
print finish
3 作業
3.1 輸入年齡進行判斷
輸入姑娘的年齡後,進行以下判斷:
1. 如果年齡小於18歲,列印“未成年”
2. 如果年齡大於18歲小於40歲,列印“青年人”
3. 如果年齡大於40歲小於60歲,列印“中年人”
4. 如果年齡大於60歲,列印“老年人”
age = int(input('請輸入年齡:'))
if age<0 or age >150:
print('不屬於年齡範圍內的數字')
else:
if age <= 18:
print('未成年')
elif age <=40:
print('青年人')
elif age <= 60:
print('中年人')
else:
print('老年人')
請輸入年齡:65
老年人
3.2 列印偶數
列印1-100之間的偶數
# fun 1
for i in range(1,101):
if i %2 == 0:
print(i,end = ' ')
i+=1
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
# fun 2
#這個代碼多少有些麻煩,改一個版本
num = 1
while num <=100:
if num % 2 == 1:
num+=1
continue
else:
print(num,end =' ')
num+=1
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
num = 1
while num<=100:
if num % 2 == 0 :
print(num,end =' ')
num+=1
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
3.3 猜年齡游戲
猜年齡游戲升級版,有以下三點要求:
1. 允許用戶最多嘗試3次
2. 每嘗試3次後,如果還沒猜對,就問用戶是否還想繼續玩,如果回答Y或y, 就繼續讓其猜3次,以此往複,如果回答N或n,就退出程式
3. 如果猜對了,就直接退出
real_age = 18
chance = 1
while True:
if chance <=3:
age = int(input("please enter the age"))
if age == 18:
print('congratulations!')
break
else:
print("sorry,guess wrong")
if age >18:
print("sorry,guess older")
else:
print("sorry,guess younger")
elif chance == 4:
choice = input('please tell me to continue<<<')
if choice == 'y'or choice =='Y':
chance = 1
continue
else:
break
chance +=1
please enter the age23
sorry,guess wrong
sorry,guess older
please enter the age14
sorry,guess wrong
sorry,guess younger
please enter the age15
sorry,guess wrong
sorry,guess younger
please tell me to continue<<<y
please enter the age14
sorry,guess wrong
sorry,guess younger
please enter the age16
sorry,guess wrong
sorry,guess younger
please enter the age17
sorry,guess wrong
sorry,guess younger
please tell me to continue<<<n
3.4 列印乘法表
for i in range(1,10):
for j in range(1,10):
if i > j:
print(f'{j}*{i}={i*j}',end=' ')
elif i == j:
print(f'{j}*{i}={i*j}',end = '\n')
else:
continue
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
# 上面的代碼,貌似有些麻煩,看了別人的覺得我的可以更改一下
for i in range(1,10):
for j in range(1,i+1):
print(f'{i}*{j}={i*j}',end =' ')
print()
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
3.5 列印金字塔
'''
# max_level=5
* # current_level=1,空格數=4,*號數=1
*** # current_level=2,空格數=3,*號數=3
***** # current_level=3,空格數=2,*號數=5
******* # current_level=4,空格數=1,*號數=7
********* # current_level=5,空格數=0,*號數=9
# 數學表達式
空格數 = max_level-current_level
*號數 = 2*current_level-1
'''
max_level=int(input("請輸入最大層數"))
for i in range(max_level):
print(f'{" "*(max_level-i-1)}',end='')
print(f'{"*"*(2*(i+1)-1)}')
請輸入最大層數5
*
***
*****
*******
*********
max_level = 5
for current_level in range(1,max_level+1):
print(f'{(max_level-current_level)*" "}{(2*current_level-1)*"*"}')
*
***
*****
*******
*********
max_level = 5
for current_level in range(1,max_level+1):
space_num = max_level-current_level
star_num = 2*current_level-1
print(f'{(space_num)*" "}{(star_num)*"*"}')
*
***
*****
*******
*********