Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 15

Python Exercise Example 15

Python 100 Examples Python 100 Examples

**Problem:** Use nested conditional operators to solve this problem: students scoring >=90 are marked A, those between 60 and 89 are marked B, and those below 60 are marked C.

**Program analysis:** (a>b) ? a:b 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

(

'

Enter 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

(

'

Enter 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:


Enter score:
89
89 属于 B

Python 100 Examples Python 100 Examples