Latest web development tutorials

Python 練習實例15

Python 100例 Python 100例

題目:利用條件運算符的嵌套來完成此題:學習成績>=90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。

程序分析:程序分析:(a>b)?a:b這是條件運算符的基本例子。

程序源代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

score = int(raw_input('input score:\n'))
if score >= 90:
    grade = 'A'
elif score >= 60:
    grade = 'B'
else:
    grade = 'C'

print '%d belongs to %s' % (score,grade)

以上實例輸出結果為:

input score:
100
100 belongs to A

Python 100例 Python 100例