Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我怎样才能找到“我的”呢;计数“;如果输入被分类在不同的部分,用户输入的类型?_Python_Python 3.x - Fatal编程技术网

Python 我怎样才能找到“我的”呢;计数“;如果输入被分类在不同的部分,用户输入的类型?

Python 我怎样才能找到“我的”呢;计数“;如果输入被分类在不同的部分,用户输入的类型?,python,python-3.x,Python,Python 3.x,我已经做了几天这个函数,这是我第一个真正的程序(我正在学习自己编写代码)。我已经使用了很多循环和while循环之类的东西,决定是时候测试一下我的技能了,我的朋友给了我一个尝试的机会。我有用户估算的“分数”,我必须在排名系统中对这些分数进行分类。 这是我迄今为止的代码: def grade_scores(): count = int(input('Enter amount of scores: ')) print('Each will be entered one per li

我已经做了几天这个函数,这是我第一个真正的程序(我正在学习自己编写代码)。我已经使用了很多循环和while循环之类的东西,决定是时候测试一下我的技能了,我的朋友给了我一个尝试的机会。我有用户估算的“分数”,我必须在排名系统中对这些分数进行分类。 这是我迄今为止的代码:

def grade_scores():
    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("GRADE  COUNT  PERCENTAGE")
    for grade in ('A', 'B', 'C', 'D', 'F'):
        return grade
        A = 'A'
        B = 'B'
        C = 'C'
        D = 'D'
        F = 'F'
    for score in (scoreList):
        if score >= 91:
            score = A
            count[0] += 1  # Increase index 0 (Corresponds to value A) by 1
        elif score >= 81 and score <=90:
            score = B
            count[1] += 1
        elif score >= 71 and score <=80:
            score = C
            count[2] += 1
        elif score >= 61 and score <=70:
            score = D
            count[3] += 1
        else: #<= 60
            score = F
            count[4] += 1

因此,我的问题是,我如何格式化我的“计数”来对用户输入进行分类,或者我是否可能在正确的轨道上

您的思路是正确的,但代码中存在一些基本缺陷

def get_grades():
    count = int(input("how many scores? "))

    scores = []
    for _ in range(count):
        score = int(input("Score: "))
        scores.append(score)
    # this whole section can be rewritten as:
    # # scores = [int(input("Score: ")) for _ in range(count)]

    result = {"A":0, "B":0, "C":0, "D":0, "F":0}
    for score in scores:
        if score > 90:
            result["A"] += 1
        elif 80 < score <= 90:
            result["B"] += 1
        elif 70 < score <= 80:
            result["C"] += 1
        elif 60 < score <= 70:
            result["D"] += 1
        else:
            result["F"] += 1
    return (scores, result)

使用字典按等级而不是数组跟踪计数。
返回第一个
for
循环中的
等级
将立即退出该函数。为什么您认为首先需要该
用于
循环中的等级?您不需要
并且无法分配到
计数[0]
count
是一个整数,因为它是用户在回答第一个问题时输入的分数。谢谢!但是我怎样才能让函数返回结果呢?我似乎得到了类型错误“unorderable types:list()scores,counts=get_grades()
自动解包。
def get_grades():
    count = int(input("how many scores? "))

    scores = []
    for _ in range(count):
        score = int(input("Score: "))
        scores.append(score)
    # this whole section can be rewritten as:
    # # scores = [int(input("Score: ")) for _ in range(count)]

    result = {"A":0, "B":0, "C":0, "D":0, "F":0}
    for score in scores:
        if score > 90:
            result["A"] += 1
        elif 80 < score <= 90:
            result["B"] += 1
        elif 70 < score <= 80:
            result["C"] += 1
        elif 60 < score <= 70:
            result["D"] += 1
        else:
            result["F"] += 1
    return (scores, result)
([the list of raw scores], {a dictionary of grade counts})