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

被折叠的 条评论
为什么被折叠?



