python3.6:如何使用list和insert方法presen构建此函数

2024-10-03 09:09:02 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次来到这个网站!我真的需要帮助修复这个函数,因为我是Python新手。我对“main_function()”中的所有这些语句都感到迷茫(我甚至不知道是否需要所有这些语句)。到目前为止,我的情况是:

# X = insert(n, X, g) - syntax
# example below:
#                n                both of these are X
# insert(['Dmitar', 52], [ ['Kane', 32], ['Arnold', 42] ], 0)
# [['Dmitar', 52], ['Kane', 32], ['Arnold', 42]]
# insert(['Dmitar', 52], [ ['Kane', 32], ['Arnold', 42] ], 1)
#[['Kane', 32], ['Arnold', 42], ['Dmitar', 52]]
# g = 0 --> [ n + X ]
# g = 1 --> [ X + n]

def insert(n, X, g):
    if g == 0:
        X.insert(0, n)
    else:
        X.append(n)

    return X

# average = avg(X) - syntax   
# returns average marks
def avg(X):
    X = []
    average = float(sum(X))/len(X)

    return X

def main_function():

    X = []

    # X = insert(n, X, g)
    # average = avg(X)

    while True:
        question = input("\nPress 's' to start with new list or 'q' to quit: ").lower()

        if question == 's':
            qurstion = input("\nInsert members into list by name ('n') or by marks ('m'): ").lower()
            # 'n' = if picked, list is ordered by name first
            # 's' = if picked, list is ordered by marks first
            if question == 'n':
                name = input("Name: ")
                marks = input("Marks: ")
                break
            X.append(question.title())
            print("X: ", X)

    #else: 

        #break
        # I really do not know where to go from here

这就是我想要的输出类型:

 Press 's' to start with new list or 'q' to quit: s
Insert members into list by name ('n') or by marks ('m'): n
Name: samantha
Marks: 45
X: [['Samantha', 45]]
Name: billy
Marks: 30
X: [['Billy', 30], ['Samantha', 45]]
Name: rudy
Marks: 75
X: [['Billy', 30], ['Rudy', 75], ['Samantha', 45]]
Name: 
AVERAGE:
Billy has a mark of: 30
Rudy has a mark of: 75
Samantha has a mark of: 45
Average: 50.0

提前感谢您的帮助!你知道吗


Tags: oroftonameinputbyiflist
1条回答
网友
1楼 · 发布于 2024-10-03 09:09:02

我同意狄克的观点,字典是更好的方法。你知道吗

grades = {}
data = []

while data != 'q':
    # Ask the user for a name.
    data = input("Please enter the student name then grades separated by a space, or 'q' to quit: ").split()
    if data[0] == 'q':
        break
    else:
        grades[data[0]] = data[1:]

class_average = 0
for k, v in grades.items():
    total = 0
    for item in v:
        total += int(item)
    average = total / len(v)
    class_average += average / len(grades)
    print("The average grade for {} is: {:0.2f}".format(k, average))

print("The class average is: {:0.2f}".format(class_average))

输出:

Please enter the student name then grades separated by a space, or 'q' to quit: Mark 90 93 96
Please enter the student name then grades separated by a space, or 'q' to quit: Ditmar 80 85 90
Please enter the student name then grades separated by a space, or 'q' to quit: John 72 74 98
Please enter the student name then grades separated by a space, or 'q' to quit: Bill 65 77 71
Please enter the student name then grades separated by a space, or 'q' to quit: Harry 42 56 63
Please enter the student name then grades separated by a space, or 'q' to quit: q
The average grade for Mark is: 93.00
The average grade for Ditmar is: 85.00
The average grade for John is: 81.33
The average grade for Bill is: 71.00
The average grade for Harry is: 53.67
The class average is: 76.80

相关问题 更多 >