简体   繁体   中英

Hi. Is there a way to write this code more briefly and avoid repeating ((if)) ? I am beginner

C:\Users\lenovo\Pictures\1400-10-04 20_12_32-untitled1 – practice.py PyCharm.png

you can improve your code adding an initial sorting and utilizing the index function. code:

your_physics_point = int(input('please insert your point: '))

#solve:
#first check
if your_physics_point not in class_points :
    print('your inserted point is NOT found!')
else:
    #sort the list decreasing
    class_points.sort(reverse=True)
    #create a dict
    scale = {1: "first", 2: "second", 3:"thrid", 4:'fourth', 5:'fifth'}
    #find the index
    ind = class_points.index(your_physics_point)
    #printing
    print('you are ' + scale[ind+1] + '!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM