Python Turtle光标不移动求助:学生成绩柱状图无法绘制
Fixing Turtle Bar Chart Not Drawing in Your Student Grade Manager
Hey there, let's troubleshoot why your turtle bar chart isn't rendering even though the window pops up. After going through your code, I found several key issues that are preventing the drawing from happening—let's break them down and fix them step by step.
Key Issues in Your Code
- Incorrect total marks collection in
getTotalMarks:
You were calculating the sum of all students' marks into a single integer, then trying to loop overrange(total)which doesn't make sense for individual student bars. We need to collect each student's total score into a list instead. - Turtle namespace conflict:
You overwrote theturtlemodule reference withturtle = turtle.Turtle()—this breaks access to module-level functions later. Use a different variable name liketfor the Turtle object. - Truncated code in
displayAStudentsDetail:
The linesum(studentMis incomplete; it should besum(studentMarkList)to calculate the student's total score. - Missing program entry point:
Your original code didn't call themenu()function to start the program, so it wouldn't run at all!
Corrected Full Code
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 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("Overall average marks:", 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): # Collect each student's total score into a list student_totals = [sum(marks) for marks in studentDetails.values()] print("All students' total marks:", student_totals) import turtle wn = turtle.Screen() # Use a different variable name to avoid overwriting the module t = turtle.Turtle() t.color("blue", "lightblue") t.penup() # Move to a starting position so bars aren't off-screen t.goto(-200, -100) t.pendown() # Loop through each student's total to draw bars for total in student_totals: t.begin_fill() # Draw a rectangle: width 20, height equal to total marks t.forward(20) t.left(90) t.forward(total) t.left(90) t.forward(20) t.left(90) t.forward(total) t.left(90) t.end_fill() # Move to next bar position with spacing t.penup() t.forward(30) t.pendown() 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): total = sum(studentMarkList) print("Student name: ", studentName) print(f"DF marks: {studentMarkList[0]}\tProject marks: {studentMarkList[1]}\tFinal exam: {studentMarkList[2]}\tTotal marks: {total}") # Start the program by calling the menu function menu()
What Changed & Why
- Fixed
getTotalMarks:- Now collects each student's total score into a list, so we can loop through and draw an individual bar for each student.
- Added a starting position for the turtle to ensure bars are visible on the screen (no more off-screen drawing!).
- Added spacing between bars for better readability.
- Resolved Turtle Namespace Conflict:
- Renamed the Turtle object to
tinstead ofturtle, so we don't overwrite the module reference.
- Renamed the Turtle object to
- Fixed Truncated Code:
- Completed the total score calculation in
displayAStudentsDetailfor cleaner output.
- Completed the total score calculation in
- Added Program Entry Point:
- Added
menu()at the end to launch the program—your original code was missing this critical step!
- Added
Now when you select option 6 from the report menu, the turtle window should draw a bar for each student's total score. If bars are too tall or short, you can adjust the scaling (e.g., multiply total by 2 in t.forward(total) to make bars taller).
内容的提问来源于stack exchange,提问作者Kelvin Gunawan
相关产品推荐
相关产品推荐

