Python Turtle:光标未移动

2024-06-02 06:43:37 发布

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

我的uni项目的代码有问题,特别是在turtle模块部分。这个程序显示学生成绩,我们应该显示一个使用海龟模块条形图。我遇到的问题是,即使turtle工作区出现,光标也不会移动以绘制条形图。你知道吗

代码如下:

def menu():

    allStudentsMarks = {}

    while True:
        userinput = input("Enter 1 to store student details \n"+
                  "Enter 2 to display student report \n"+
                  "Enter 3 to exit \n")
        if userinput == "1":
            enterMarks(allStudentsMarks)
        elif userinput == "2":
            displayReports(allStudentsMarks)
        elif userinput == "3":
            print("You have left the program. Thank you.")
            break

def enterMarks(allStudentsMarks):
    studentName = input("Please enter the student's name: ")

    while True:
        DFMarks = input("Please enter the marks from 0-20: ")
        DFMarks = float(DFMarks)
        if DFMarks >= 0 and DFMarks <= 20:
            break

    while True:
        ProjectMarks = input("Please enter the marks from 0-30: ")
        ProjectMarks = float(ProjectMarks)
        if ProjectMarks >= 0 and ProjectMarks <= 30:
            break

    while True:
        FinalMarks = input("Please enter the marks from 0-50: ")
        FinalMarks = float(FinalMarks)
        if FinalMarks >= 0 and FinalMarks <= 50:
            break

    marksList = [DFMarks, ProjectMarks, FinalMarks]
    allStudentsMarks[studentName] = marksList
    studentDetails = sum(marksList)

def getBelowAvgDFMarks(studentDetails):
    marks = list(studentDetails.values())
    dfTot = 0
    dfLen = 0
    for marksList in marks:
        dfTot += marksList[0]
        dfLen += 1
    dfAvg = dfTot / dfLen
    print("Displaying the student(s) whose DF marks are below the average of", dfAvg)

    belowAvgDF = {}
    for student in studentDetails:
        if studentDetails[student][0] < dfAvg:
                belowAvgDF[student] = studentDetails[student][0]
    print(belowAvgDF)
    return

def getBelowAvgProjectMarks(studentDetails):
    marks = list(studentDetails.values())
    projectTot = 0
    projectLen = 0
    for marksList in marks:
        projectTot = projectTot + marksList[1]
        projectLen = projectLen + 1
    projectAvg = projectTot / projectLen
    print("Displaying the student(s) whose project marks are below the average of", projectAvg)

    belowAvgProject = {}
    for student in studentDetails:
        if studentDetails[student][1] < projectAvg:
            belowAvgProject[student] = studentDetails[student][1]
    print(belowAvgProject)
    return

def getBelowAvgFinalExamMarks(studentDetails):
    marks = list(studentDetails.values())
    finalTot = 0
    finalLen = 0
    for marksList in marks:
        finalTot = finalTot + marksList[2]
        finalLen = finalLen + 1
    finalAvg = finalTot / finalLen
    print("Displaying the student(s) whose final marks are below the average of", finalAvg)

    belowAvgFinal = {}
    for student in studentDetails:
        if studentDetails[student][2] < finalAvg:
            belowAvgFinal[student] = studentDetails[student][2]
    print(belowAvgFinal)
    return

def getBelowAvgOverallMarks(studentDetails):
    marks = list(studentDetails.values())
    overall = 0
    overallLen = len(marks)
    for marksList in marks:
        for num in marksList:
            overall += num
    overallAvg = overall / overallLen
    print(overallAvg)
    print("Displaying the student(s) whose overall marks are below the average of", overallAvg)

    belowAvgOverall = {}
    for student in studentDetails:
        if sum(studentDetails[student]) < overallAvg:
            belowAvgOverall[student] = sum(studentDetails[student])
    print(belowAvgOverall)
    return

def getTotalMarks(studentDetails):
    total = []
    marks = list(studentDetails.values())
    total = 0
    for i in marks:
        for num in i:
            total = total + num
        print(total)
        total = 0

    import turtle
    wn = turtle.Screen()
    turtle = turtle.Turtle()
    turtle.color("blue", "lightblue")
    for marks in range(total):
        turtle.begin_fill()
        turtle.forward(10)
        turtle.left(90)
        turtle.forward(marks)
        turtle.left(90)
        turtle.forward(10)
        turtle.left(90)
        turtle.forward(marks)
        turtle.left(90)
        turtle.end_fill()
        turtle.forward(20)

    wn.exitonclick()


def displayReports(allStudentsDetails): 
    userinput = input("Enter 1 to get the average DF report marks \n"+
                   "Enter 2 to get the average project report marks \n"+
                   "Enter 3 to get the average final exam marks \n"+
                   "Enter 4 to get the overall marks \n"+
                   "Enter 5 to get the selected student marks \n"+
                  "Enter 6 to get the bar chart of the total marks \n")
    if userinput == "1":
        getBelowAvgDFMarks(allStudentsDetails)
    elif userinput == "2":
        getBelowAvgProjectMarks(allStudentsDetails)
    elif userinput == "3":
        getBelowAvgFinalExamMarks(allStudentsDetails)
    elif userinput == "4":
        getBelowAvgOverallMarks(allStudentsDetails)
    elif userinput == "5":
        displaySelectedStudentsMarks(allStudentsDetails)
    elif userinput == "6":
        getTotalMarks(allStudentsDetails)
        return

def displaySelectedStudentsMarks(selectedStudentsDetails):
    for studentNameKey in selectedStudentsDetails:
        displayAStudentsDetail(studentNameKey, selectedStudentsDetails[studentNameKey])

def displayAStudentsDetail(studentName, studentMarkList):
    print("Student name: ", studentName)
    print("DF marks: ", studentMarkList[0], "\tProject marks: ", studentMarkList[1], "\tFinal exam: ", studentMarkList[2], "\tTotal marks: ", sum(studentMarkList))


menu()

当您输入值'6'(“获取总分条形图”)时,在第二级“显示学生报告”菜单中,它应该能够显示每个学生的总分条形图。你知道吗

谢谢你。你知道吗


Tags: thetoinforifdefstudenttotal
1条回答
网友
1楼 · 发布于 2024-06-02 06:43:37

我相信你的getTotalMarks()真是一团糟。与其说是海龟,不如说是Python。一个线索是有两个同名的不同变量:

total = []
marks = list(studentDetails.values())
total = 0

我建议做三个改变。首先,以这种方式导入海龟:

from turtle import Turtle, Screen

其次,用以下方式启动menu()例程:

wn = Screen()

menu()

wn.exitonclick()

也就是说,不要把exitonclick()放在一个函数里面,让它成为你的代码做的最后一件事。最后,用这个重写替换getTotalMarks()

def getTotalMarks(studentDetails):
    totals = [sum(scores) for scores in studentDetails.values()]

    turtle = Turtle()
    turtle.color("blue", "lightblue")

    for total in totals:
        turtle.begin_fill()
        turtle.forward(10)
        turtle.left(90)
        turtle.forward(total)
        turtle.left(90)
        turtle.forward(10)
        turtle.left(90)
        turtle.forward(total)
        turtle.left(90)
        turtle.end_fill()

        turtle.forward(20)

这没什么特别的,但它会让你向前迈进。你知道吗

相关问题 更多 >