python 分支,while,for等基礎

2021-09-11 06:54:21 字數 3772 閱讀 1296

#bool(布林)

a=''

print(bool(a))

b=none

print(bool(b))

a=1print(bool(a))

salary=int(input("請輸入薪資:"))

if salary>10000:

print("可以買一輛邁騰")

if salary>5000 and salary<10000:

print("博越")

#單 if

cj=int(input("請輸入成績:"))

if cj>=90:

print("優秀")

if cj>=80 and cj<90:

print("良好")

if cj>=60 and cj<80:

print("及格")

if cj<60:

print("不及格")

#雙分支

cet4=int(input("請輸入四級成績:"))

if cet4>425:

print("頒發證書")

else:

print("請繼續加油")

***=input("請輸入性別")

if ***=='男':

print("送刮鬍刀")

print("送皮鞋")

else:

print("送化妝品")

#多分支

score=int(input("請輸入成績:"))

if score>=90 and score<=100:

print("優秀")

#下乙個elif變為if 結果是有區別的

elif score>=80: #elif==else if

print("良好")

elif score>=60:

print("及格")

else:

print("不及格")

print("程式結束")

money=int(input("請輸入購買金額:"))

***=input("請輸入性別:")

if money>=3000:

if ***=='男':

print("送手錶")

elif ***=='女':

print("送化妝品")

elif money<3000:

if ***=='男':

print("送打火機")

elif *** == '女':

print("送發卡")

price=float(input("請輸入西瓜單價:"))

num=float(input("請輸入西瓜斤數:"))

money=price*num

if money>=300:

vip=int(input("請輸入vip級別:"))

if vip>=1:

money*=0.8

print("折扣後:%.2f"%(money)) # 他與print("折扣後:",money)功能一樣,句式不一樣

else:

print("應付錢:%.2f"%(money))

elif money<300:

***=input("請輸入性別:")

print("付錢:",money)

if ***=='男':

print("送蘋果乙個")

else:

print("送發卡乙個")

#while 吃瓜

money=int(input("請輸入錢:"))

while money>=10:

money-=10

#print("吃西瓜還剩下:",money,"元")

print("最後還剩:",money,"元")

#while從1到100的和

s=0

i=1while i<=100:

s+=i

i+=1 #步長為1

print(s)

#for 從1到100的和

s=1

i=50

while i>=30:

s*=i

i-=3

print(s)

i=50

while i>=20:

if i%3==0 or i%7==0:

print(i)

i-=1

#break range

#for

#從1到100的和

he=0

for i in range(1,101):

he+=i

print(he)

#找出200到300間所有個位加十位和為10的數

a=200

while a<=301:

gw=a%10

sw=a//10%10

he=gw+sw

if he==10:

print(a)

a+=1

#8 死迴圈 break continue

#輸入十個人的成績,當有<0時,結束迴圈,提示異常

i=1

while i<=10:

score=int(input("請輸入第%d人的成績:"%(i)))

if score<0:

print("異常")

break

i+=1

#雙重迴圈畫五行五列心

i=1

while i<=5:

j=1while j<=5:

print("*",end="")

j+=1

print()

i+=1

#三角形心

i=1

while i<=4:

j=1while j<=i:

print("*",end="")

j+=1

print()

i+=1

#求證質數

a=int(input("請輸入乙個數:"))

i=2while i#乘法口訣:

line=1

while line<=9:

colume=1

while colume<=line:

print(colume,"*",line,"=",colume*line,end='\t')

colume+=1

print()

line+=1

line=1

while line<=9:

colume=1

while colume<=line:

print("{}*{}={}".format(colume,line,colume*line),end='\t')

colume+=1

print()

line+=1

line=1

while line<=9:

colume=1

while colume<=line:

print("%d*%d=%-2d"%(colume,line,colume*line),end='\t')

colume+=1

print()

line+=1

python分支語句教案 Python分支語句if

4 分支語句if 前述章節介紹了資料 表示式和語句等基本概念,程式的基本組成單位是語句。4.1 程式的基本結構 寫入程式裡的各條語句如果在程式被執行時一條條按順序執行各條語句,那麼這個程式的結構稱之為順序型程式,即每條語句的執行按其書寫時的位置依次執行。順序型結構的程式一般適用於較為簡單的應用環境或...

Python 分支結構

分支結構示例1 pm2.5.ipynb pm input 請輸入pm2.5的值 print pm print type pm eval函式功能 將字串str當成有效的表示式來求值並返回計算結果 pm1 eval pm if pm1 75 print 空氣汙染警報 if pm1 35 print 空氣...

python分支語句

判斷語句 if if 要判斷的條件 條件成立的時,要做的事情 else 條件不成立的時候,要做的事情 注意 的縮排為乙個tab鍵,或者四個空格 tab鍵和空格不要混用 age 13 if age 18 print 允許進入網咖 else print 未成年,不允許進入網咖 print 邏輯運算子 a...