美文网首页
Python教程(三)

Python教程(三)

作者: Wcy100 | 来源:发表于2017-01-03 18:11 被阅读10次

条件判断

  • 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)

相关文章

网友评论

      本文标题:Python教程(三)

      本文链接:https://www.haomeiwen.com/subject/uxzfettx.html