5.判斷和迴圈

来源:https://www.cnblogs.com/csfy0524/p/18392333
-Advertisement-
Play Games

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)*"*"}')
    *
   ***
  *****
 *******
*********


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Spring之IOC 簡介 首先,官網中有這樣一句話:Spring Framework implementation of the Inversion of Control (IoC) principle.這句話翻譯過來就是:Spring實現控制反轉(IOC)原理,由此可以得出,Inversion ...
  • 寫在前面 1447 字 | 感觸 | 見聞 | 孩子 | 童真 | 天性 正文 晚上。跳舞。 老師有很多學生,幾乎都念小學,大的不過五年級。小孩子。 今晚只來了一個。其他的孩子據說都在寫作業。 這幾天老師都打算回歸街舞本源,去街上跳舞,於是讓我們大包小包拿東西。 音箱、地膠、小型探照燈,水杯什麼的。 ...
  • 概述 租約機制指在租約期限內,擁有租約的節點有權利操作一些預設好的對象,具體如下 租約是由授權者授予的一段時間內的承諾 授權者一旦發出租約,則無論接受方是否收到,也無論後續接收方處於何種狀態,只要租約不過期,授權者就得遵守承諾,按承諾的時間和內容執行。 接收方在有效期內可以使用授權者的租約,如果租約 ...
  • 單欄位和多欄位重寫hashcode 在 Java 中,重寫 hashCode 方法的場景通常與對象的哈希值計算有關,特別是在使用哈希表(如 HashMap, HashSet 等)時。下麵是你提供的兩種 hashCode 實現的具體使用場景分析: 1. 第一種實現 @Override public b ...
  • OpenCV(Open Source Computer Vision Library)是一個開源的電腦視覺和機器學習軟體庫,旨在提供一個跨平臺的、易於使用的、快速執行的電腦視覺介面。如果只是簡單的使用,其實不必要像筆者這樣使用源代碼進行構建,直接使用官方提供的二進位安裝包即可。一般來說,需要從源 ...
  • 寫在前面 4002 字 | 陪伴 | 親密關係 | 患難與共 《理想雪》系列故事均為架空世界觀,所有人名、地名等與現實世界無任何關聯。 該系列只且僅只為了說明,小說作者在該情境下會誕生的想法和採取的行動,以及背後的世界觀、價值觀和人生觀。因此將具有強烈的個人風格。 未經授權,禁止轉載。僅供小範圍內閱 ...
  • 帶箭頭的直線就是有方向的直線,既可以用來表示矢量,也可以用來標記某個關鍵位置。manim中提供了4種常用的帶箭頭的直線模塊: Arrow:單箭頭的直線 DoubleArrow:雙箭頭的直線 LabeledArrow:帶標簽的直線 Vector:向量 其中,DoubleArrow,LabeledArr ...
  • 六,Spring Boot 容器中 Lombok 插件的詳細使用,簡化配置,提高開發效率 @目錄六,Spring Boot 容器中 Lombok 插件的詳細使用,簡化配置,提高開發效率1. Lombok 介紹2. Lombok 常用註解2.1 @ToString2.2 @Setter2.3 @Dat ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...