簡體   English   中英

如何對輸入列表進行排名並為其分配變量以返回給用戶?

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

我的程序很難,我的代碼技能很基礎。 我需要做的是從用戶那里輸入一個輸入列表,並按字母A(66-100),B(33-65),C(0-32)對其進行排名。 我假設我需要輸入的列表是一個元組,但是我不確定如何做到這一點。 我知道我需要(或可以)使用省略號來完成此操作,但是我不確定如何使B成為兩個數字之間的范圍,因為C等於其他值,而A大於。 這是我的代碼:

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')

只需使用邏輯運算符( and or ,, not )將您的條件鏈接在一起,然后遍歷列表中的每個項目:

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')

if結構的可能解決方案:

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

這樣,您可以使用else處理不介於0100之間的輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM