Python Turtle:光标没有移动

时间:2018-05-24 04:59:55

标签: python turtle-graphics

我的uni项目代码存在问题,尤其是在海龟模块部分。该程序显示学生标记,我们应该使用海龟模块显示条形图。我遇到的问题是,即使乌龟工作区出现,光标也不会移动以绘制条形图。

这是代码:

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

当您输入值&#39; 6&#39; (&#34;得到总分的条形图&#34;),在第二级&#34;显示学生报告&#34;菜单,它应该能够显示每个学生的总考试价值的条形图。

谢谢。

1 个答案:

答案 0 :(得分:0)

我相信你的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)

这没什么特别的,但它应该让你前进。

相关问题