简体   繁体   中英

How can I rank a input list and assign it a variable to return to user?

I'm having a hard time with my program and my code skills are quite elementary. What I need to do is take an inputted list from the user and rank them by letter A (66 - 100), B (33 - 65), C (0 - 32). I assume I need the inputted list to be a tuple but I'm not entirely sure how to do so. I know I need to (or can) use an elif to accomplish this but I'm not sure how to make it a range between two numbers for B since C is else, and A is just greater than. This is my code:

def scores():
    print('we are starting')
    count = int(input('Enter amount of scores: '))
    print('Each will be entered one per line')
    scoreList = []
    for i in range(1, count+1):
            scoreList.append(int(input('Enter score: ')))
            print(scoreList)
    print(scoreList)
    if scoreList > 66:
        print('A')
    #elif scoreList > 33:
        #print('B')
    else:
        print ('C')

Just chain together your conditions using logical operators ( and , or , not ) and loop through each item in the list:

def scores():
    print('we are starting')
    count = int(input('Enter amount of scores: '))
    print('Each will be entered one per line')
    scoreList = []
    for i in range(1, count+1):
            scoreList.append(int(input('Enter score: ')))
            print(scoreList)
    print(scoreList)
    for score in scoreList:
        if score >= 66:
            print('A')
        elif score >= 35 and score <=65:
            print('B')
        else:
            print('C')

A possible solution for the if-structure:

for score in scoreList:
    if 66 <= score <= 100:
        print('A')
    elif 33 <= score <= 65:
        print('B')
    elif 0 <= score <= 32:
        print('C')
    else:
        # handle out of range input

This way you can use the else to handle input that isn't between 0 and 100 .

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