Python教程(三)

条件判断

  • if-else

说明:elif 是 else if 的缩写,在每一个 if、elif(else if)、else 语句后面都要加上冒号

  • 示例代码
scoreStr = input("Please input your score:");
score = int(scoreStr);
PASS_SCORE = 60;
WELL_SCORE = 80;
EXCELLENT_SCORE = 90;
MAX_SCORE_LIMIT=100;
MIN_SCORE_LIMIT=0;
if score<MIN_SCORE_LIMIT:
    print("Invalid score : less than min score limitation.")
elif score<PASS_SCORE:
    print("Sorry,you've failed to pass the exam.")
elif score<WELL_SCORE:
    print("Congratulations,you've passed the exam.")
elif score<EXCELLENT_SCORE:
    print("Pretty good,your score is favorable.")
elif score<MAX_SCORE_LIMIT:
    print("You've got a excellent score.")
elif score==MAX_SCORE_LIMIT:
    print("You've got the full mark.")
else:
    print("Invalid score : greater than max score limitation.")

循环

  • for
  • 示例代码
numberSeq=[1,3,5,7,8]
sum = 0
for x in numberSeq:
    sum+=x
print("sum=%d" % sum)
  • while
  • 示例代码
n = 100
sum = 0
while n>0 :
    sum+=n
    n-=1
print("sum=%d" % sum)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容