![57d1dd096c15bd2e17d6c8b278011985.png](https://i-blog.csdnimg.cn/blog_migrate/e38dc59525c26e57498e602924d3730a.jpeg)
传统化交易还是有些人性无法避免的弊端,这个时候,我们就要借助计算机进行程序化交易,让我们的交易走得更高更远。而程序化交易必备的知识是Python,下面我们讲解下python中的if语句。
只要你耐心看完这篇文章,我相信你一定会对if的用法清楚了解,不会再对它云里雾里。比如说if的嵌套。
按照100分制,90分以上成绩为A,80-90为B,60-80为C,60以下为D。
写一个程序,使当输入分数时,屏幕上显示出相应的形式。
一共有三种方法。(具体代码可见下文)
第一种:
![1443d0b1aadff822d37dc96dd2d1688d.png](https://i-blog.csdnimg.cn/blog_migrate/1e73a155ab23eeeff45795bd1fda2059.jpeg)
第二种:
![ae7edf73c31799aed340f88b196c9305.png](https://i-blog.csdnimg.cn/blog_migrate/61d3d6d22767f13052c811aecc05cacf.jpeg)
第三种:
![f2b136b47c4e02266d4b13f960611ed2.png](https://i-blog.csdnimg.cn/blog_migrate/ddbc40747050be3fee44c15537a41131.jpeg)
如果你能自己在Python上敲打并执行代码,你一定会对if语句有个特别深刻清晰的理解。
方法一的特点:5条if语句并列平行,若第一条语句已执行,程序还会继续执行至代码结束。比如分数是90,则显示出A后,程序还会继续执行。它其实很浪费CPO的时间。方法二和方法三则避免了这种问题。
其中,方法三中的elif=else if() ,它很好得体现了python的简洁与可读性强。
方法一代码:
score=int(input('请输入一个分数:'))
if 100>=score>=90:
print('A')
if 80<=score<90:
print('B')
if 60<=score<80:
print('C')
if 0<=score<60:
print('D')
if score>100 or score<0:
print('输入错误!')
方法二代码:
score=int(input("请输入您的分数:"))
if 90<=score<=100:
print('A')
else:
if 80<=score<90:
print('B')
else:
if 60<=score<80:
print("C")
else:
if 0<=score<60:
print('D')
else:
print('输入错误!')
方法三代码
score=int(input('请输入一个分数:'))
if 100>=score>=90:
print('A')
elif 80<=score<90:
print('B')
elif 60<=score<80:
print('C')
elif 0<=score<60:
print('D')
else:
print('输入错误!')