python的语句_Python的语句
python中的兩種語句:
1、if條件控制語句
格式:
if:
a = int(input("請輸入第一個數:"))
b = int(input("請輸入第二個數:"))
if a < b:
print("a比b小")
if-else:
a = int(input("請輸入第一個數:"))
b = int(input("請輸入第二個數:"))
if a < b:
print("a比b小")
else:
print("a比b大")
a = int(input("請輸入一個三位數:"))
#153? 水仙花數含義:153=1^3 + 5^3 + 3^3
q = a % 10
w = a // 10 % 10
e = a //100
if a == q**3 + pow(w,3) + e**3:
print("是水仙花數")
else:
print("不是水仙花數")
if-elif-else:
age = int(input("請輸入您的年齡:"))
if age <= 0 :
print("娘胎里")
elif age < 3 :
print("兒童")
elif age < 6 :
print("少年")
elif age < 18 :
print("成年")
elif age < 30 :
print("青年")
elif age < 50 :
print("中年")
elif age < 100 :
print("老年")
elif age < 150 :
print("老壽星")
else:
print("老妖怪")
2、while、for循環語句
while語句:
while 表達式:
語句1
邏輯:當程序執行到while語句時,首先計算表達式的式的值,當表達式的值為假,結束整個語句,如果表達式的值為真,則執行語句,執行完語句再次計算表達式的值
while 表達式:
語句1
else:
語句2
邏輯:當表達式執行為false時,執行else
def BMI():
if bmi < 18.5:
# 下面 2 行同屬于 if 分支語句中包含的代碼,因此屬于同一作用域
print("BMI指數為:" + str(bmi))? # 輸出BMI指數
print("體重過輕")
if bmi >= 18.5 and bmi < 24.9:
print("BMI指數為:" + str(bmi))? # 輸出BMI指數
print("正常范圍,注意保持")
if bmi >= 24.9 and bmi < 29.9:
print("BMI指數為:" + str(bmi))? # 輸出BMI指數
print("體重過重")
if bmi >= 29.9:
print("BMI指數為:"+str(bmi)) #輸出BMI指數
print("肥胖")
age = int(input("請輸入您當前的年齡"))
while age >= 18 and age <= 25:
h = float(input("請輸入您當前身高"))
w = float(input("請輸入您當前體重"))
bmi = w / h ** 2
BMI()
age += 1
else:
print("我們只看18-25歲的哦")
for語句:
for 變量名 in? 集合:
語句
邏輯:按順序取集合中的每個元素,賦值給變量,再去執行語句,如此循環往復,直到取完集合中的元素截止
def BMI():
if bmi < 18.5:
# 下面 2 行同屬于 if 分支語句中包含的代碼,因此屬于同一作用域
print("BMI指數為:" + str(bmi))? # 輸出BMI指數
print("體重過輕")
if bmi >= 18.5 and bmi < 24.9:
print("BMI指數為:" + str(bmi))? # 輸出BMI指數
print("正常范圍,注意保持")
if bmi >= 24.9 and bmi < 29.9:
print("BMI指數為:" + str(bmi))? # 輸出BMI指數
print("體重過重")
if bmi >= 29.9:
print("BMI指數為:"+str(bmi)) #輸出BMI指數
print("肥胖")
a = int(input("請輸入您的年齡"))
for age in range(18,25):
#print(age)
if a == age:
h = float(input("請輸入身高"))
w = float(input("請輸入體重"))
bmi = w / h ** 2
BMI()
a += 1
else:
print("我們只看18-25歲的哦")
總結
以上是生活随笔為你收集整理的python的语句_Python的语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python调用js获取异步返回的数据_
- 下一篇: qc成果报告范例_QC推进这样做才是货真