Python 1-07 条件与循环语句

一、条件语句

1、if <条件>:

age = int(input('Please enter your age:'))

if age >= 18:
    print(f' Your age is { age } , and you are an adult with Internet access.')
else:
    print(f'Your age is { age } , please go home and do your homework.')

2、多重判断

score = int(input('Please enter your scores:'))
if score < 60:
    print(f'Your score is a {score}, grade is "E" .')  
elif (score < 70):
	print(f'Your score is a {score}, grade is "D" .')  
elif (score < 80):
	print(f'Your score is a {score}, grade is "C" .')  
elif (score < 90):
	print(f'Your score is a {score}, grade is "B" .')  
else:
	print(f'Your score is a {score}, grade is "A" .')  

Python 没有分支(switch)语句,可以使用多重条件语句实现。

3、三目运算符

<

猜你喜欢

转载自blog.csdn.net/weixin_43955170/article/details/112545898