Small example of python2.7 practice (14)

    14): Question: Use the nesting of conditional operators to complete this question: students with grades >= 90 are represented by A, those with scores between 60 and 89 are represented by B, and those with scores below 60 are represented by C.

    Program Analysis: Program Analysis: (a>b)?a:b This is a basic example of a conditional operator.

    Program source code:

#!/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 )

             

    The output of the above example is:

Input Score: 89 89 is B

 

    Use ranges:

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


def k(x):if x in range(60):print('C')elif x in range(60,90):print('B')else:print('A')
score =int(raw_input('输入分数:\n'))
k(score)
    
        
    
        
    
         
    
#!/user/bin/python# -*- coding: UTF-8 -*-


a = int ( raw_input ( 'input score:' )) # first method print 'A' if a > 89 else ( 'B' if a > 59 else 'C' ) # second method print 'A' and a > 89 or 'B' and a > 59 or 'C'

       

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


i = int ( input ( 'Please enter the grade:' )) 
ar = [ 90 , 60 , 0 ] 
res = [ 'A' , 'B' , 'C' ] for idx in range ( 0 , 3 ): if i >= ar [ idx ]: print ( res [ idx ]) break   

    
        
        

    Enter on the premise of 0-100:

# coding:utf-8 
score = int ( raw_input ( 'input score:\n' )) print ([ 'C' , 'C' , 'B' , 'A' ][ score / 30 ]) 

    Reference method, compatible with Python3.x and Python2.x:

# -*- coding: UTF-8 -*-

score = [ 80 , 78 , 90 , 48 , 62 ] #The grades of 5 students 
grade = [] #Used to store grades                        

for i in range(5):
    s =(score[i]>=90)and'A'or((score[i]>=60)and'B'or'C')
    grade.append(s)       

print(grade) 

    This example is relatively simple, so I won't go into too much detail. . .

    If you feel good, please like and support more. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324683227&siteId=291194637