條件判斷
- if-else
說明:elif 是 else if 的縮寫,在每一個(gè) 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.")
循環(huán)
- 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)