Python Exercise Example 15
Problem:Use nested conditional operators to solve this problem: students with a score >=90 are graded A, those between 60-89 are graded B, and those below 60 are graded C.
Program analysis:Program analysis:(a>b) ? a:bThis is a basic example of the conditional operator.
Program source code:
Example (Python 2.x)
#!/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)
Example (Python 3.x)
#!/usr/bin/python3
score = int(input('Input score:\n'))
if score >= 90:
grade = 'A'
elif score >= 60:
grade = 'B'
else:
grade = 'C'
print ('%d belongs to %s' % (score,grade))
The output of the above example is:
输入分数: 89 89 属于 BOther extensions
Python 100 Examples