云学编程的第11天—【微软官方python入门教程 P23-P24笔记】2021-11-11 and和布尔标记

本文探讨了如何在编程中使用AND运算符来同时满足两个条件(如GPA大于等于85%且最低成绩不低于70%),并介绍了使用布尔变量作为标志的重要性。通过实例演示了如何检查学生是否达到荣誉名单标准。

 P23 and和布尔标记

 

Sometimes you can combine conditions with AND instead of nesting if statements

[ requirements for honour roll

`Minimum 85% grade point average

`Lowest grade is at least 70% ]

if gpa >= .85: and lowest_grade >= .70:
    print('Well done')

How AND statements are processed

The way and statements are processed is both conditons must be true for the condition to be evaluated as true.

If you need to remember the results of a condition check later in your code, use Boolean variables as flags

if gpa >= .85 and lowest_grade >= .70:
    honour_roll = True
else:
    honour_roll = False
# Somewhere later in your code
if honour_roll:
    print('well done')

P24实操

`AND

# A student makes honour roll if their average is >= 85
# and their lowest grade is not below 70
gpa = float(input('What was your grade point Average?'))
lowest_grade = float(input('What was your lowest grade?'))

if gpa >= .85 and lowest_grade >= .70:
    print('You made the honour roll')

 

`True  False 或者 1 0

gpa = float(input('What was your grade point Average?'))
lowest_grade = float(input('What was your lowest grade?'))
if gpa >= .85 and lowest_grade >= .70:
    honour_roll = True
else:
    honour_roll = False
 #later in your code if you need to check 
if honour_roll:
    print('You made the honour roll')
if gpa >= .85 and lowest_grade >= .70:
    honour_roll = 1
else:
    honour_roll = 0

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值