Python的语句---钱中平
2024-09-05 01:05:00
python中的两种语句:
1、if条件控制语句
1、if条件控制语句
格式:
if:
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
if a < b:
print("a比b小")
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大")
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("不是水仙花数")
#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("老妖怪")
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语句时,首先计算表达式的式的值,当表达式的值为假,结束整个语句,如果表达式的值为真,则执行语句,执行完语句再次计算表达式的值
语句1
逻辑:当程序执行到while语句时,首先计算表达式的式的值,当表达式的值为假,结束整个语句,如果表达式的值为真,则执行语句,执行完语句再次计算表达式的值
while 表达式:
语句1
else:
语句2
语句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("肥胖")
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岁的哦")
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("肥胖")
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岁的哦")
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岁的哦")
最新文章
- CSS魔法堂:重拾Border之——更广阔的遐想
- Apache Marmotta 3.1.0-incubating 发布
- 遍历所有的选中的radio的个数和值
- Java MD5加密工具类
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
- PAT (Basic Level) Practise:1008. 数组元素循环右移问题
- cxf2.7.10与Spring3.0.5集成时报错如下
- 第3.3.4节 创建高级图形之RenderScript(二)
- 维吉尼亚密码java代码实现根据密钥长度计算IC值过程
- ubuntu上安装apache2+mysql+php5-fpm(PHP5 - FastCGI Process Manager)
- 解决:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression &#39;requestMap.maintenancename != null and requestMap.maintenance
- cocos 动画系统
- centos7安装与配置nginx1.11,开机启动
- Pytorch报错记录
- 根据CAS协议写的简单的SSO框架
- ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(4)
- 分享微信开发Html5轻游戏中的几个坑
- Nuget包CommonServiceLocator从1.0.3升级到2.0.4时MvvmLight的ViewModelLocator初始化SimpleIoc.Default格式不匹配问题
- linux中awk工具的使用(转载)
- Android中getLocationOnScreen和getLocationInWindow 获取屏幕大小