python for循环连续输入五个成绩判断等级_Python条件循环判断
1.條件判斷語句
Python中條件選擇語句的關鍵字為:if 、elif 、else這三個。其基本形式如下:
1
2
3
4
5
6
7
8
9
age_of_cc= 27
age= int(input("guessage:"))
if age== age_of_cc:
print("Yes,you got it!")
elif age > age_of_cc:
print("猜大啦!")
else:
print("猜小啦!")
if語句執行的特點是從上往下判斷;
其中elif和else語句塊是可選的。對于if和elif只有判斷為True時,該分支語句才執行,只有當if和所有的elif的判斷都為False時,才執行else分支。注意Python中條件選擇語句中判斷后面有個冒號。
1 AA = input(">>>:") #輸入 aa 或 bb
2 if (AA == "aa","bb"): #滿足小括號中任意一個條件都會執行
3 print('輸出:',AA)4
5 或6
7 AA = input(">>>:") #輸入 aa 或 bb
8 if AA in ("aa","bb"): #滿足小括號中任意一個條件都會執行
9 print('輸出:',AA)
2.循環語句
2.1 while循環
用法:
1
2
while 條件:
xxxxxx
while會不停地循環執行隸屬于它的語句,直到條件為假(False)
2.1.1 break跳過循環
代碼示例:
age_of_cc = 27
count =0
while count < 3:
age = int(input("guessage:"))
if age == age_of_cc:
print("Yes,you got it!")
break
elif age > age_of_cc:
print("猜大啦!")
else:
print("猜小啦!")
count += 1
else:
if count == 3:
print("錯誤太多次啦!")
2.1.2 continue跳過該次循環
代碼示例:
1
2
3
4
5
6
i= 1
while i <10:
i+= 1
if i%2 >0:# 非雙數時跳過輸出
continue
print(i)# 輸出雙數2、4、6、8、10
2.2 for循環
for循環需要預先設定好循環的次數(n),然后執行隸屬于for的語句n次。
代碼示例:
1
2
for iin range(10):
print(i)#輸出0 1 2 3 4 5 6 7 8 9
while循環判斷語句代碼示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
age_of_cc= 27
count=0
while count <3:
age= int(input("guessage:"))
if age== age_of_cc:
print("Yes,you got it!")
break
elif age > age_of_cc:
print("猜大啦!")
else:
print("猜小啦!")
count+= 1
else:
if count== 3:
print("錯誤太多次啦!")
for條件判斷代碼示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
age_of_cc= 27
count= 0
for iin range(3):
age= int(input("guessage:"))
if age== age_of_cc:
print("Yes,you got it!")
break
elif age > age_of_cc:
print("猜大啦!")
else:
print("猜小啦!")
count+= 1
else:
if count== 3:
print("錯誤太多次啦!")
3.1 input
input是輸入函數,用戶可以輸入字符串保存到變量中
代碼示例:
1
name= input("Please input your name")
3.2 print
用print()在括號中加上字符串,就可以向屏幕上輸出指定的文字
代碼示例:
1
print("Hello!")
3.3 類型轉換
通過上文可以看出,input輸入的在python中都會被認為是字符串(見下圖),所以我們需要對input的內容進行類型轉換:
轉換成int示例:
1
age= int(input("age is:"))
轉換回字符串:str()
總結
以上是生活随笔為你收集整理的python for循环连续输入五个成绩判断等级_Python条件循环判断的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux mysql安装_LINUX
- 下一篇: websocket python爬虫_p