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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)