代码为不受支持的操作数返回Traceback Error看不到问题

问题描述

当前正在做作业。我的代码返回了几行代码中的错误,并暗示我正在使用Tkinter,而我还没有。寻找任何有时间关注我的代码的人。

为了能够看到错误,我按下“ d”打开输入窗口,然后键入“ square”(全部小写)。

当您输入程序名称或具有的边数时,我正在尝试使程序绘制形状。

我的错误返回为:

    <header>
        <div class="logo-text">Cloudnotes</div>
        <button class="create-account">Create an Account</button>
        <a class="sign-in">Sign in</a>
    </header>
    <div id="block1">
      
    </div>
    <div id="block2">
    
    </div>
  </body>

这是我的代码

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\python36\lib\tkinter\__init__.py",line 1699,in __call__
    return self.func(*args)
  File "C:\Program Files\python36\lib\turtle.py",line 701,in eventfun
    fun()
  File "E:\Master 200820 no comments Stack.py",line 85,in startUp
    mainGame()
  File "E:\Master 200820 no comments Stack.py",line 125,in mainGame
    regShapeDraw()
  File "E:\Master 200820 no comments Stack.py",line 73,in regShapeDraw
    length = ((1 / sides) * 500)
TypeError: unsupported operand type(s) for /: 'int' and 'list'

解决方法

我的代码在几行代码中返回错误,并且 提示我正在使用Tkinter,而我没有。

如果您使用的是 turtle ,则您使用的是 tkinter ,因为turtle位于tkinter之上。至于错误:

File "E:\Master 200820 no comments Stack.py",line 73,in regShapeDraw
    length = ((1 / sides) * 500)
TypeError: unsupported operand type(s) for /: 'int' and 'list'

在代码中的任何地方,都将变量sides视为int

angle = (360 / sides)
for i in range(sides - 1):
sides = 3
sides = 4
...
sides = 11
sides = 12
sides = int(userInput)

除了它的全局值:

sides = []

这导致下一个问题。函数regShapeDraw()全局使用sides

def regShapeDraw():
    length = ((1 / sides) * 500)
    angle = (360 / sides)
    pen.forward(length / 2)
    for i in range(sides - 1):
        pen.left(angle)
        pen.forward(length)
    pen.left(angle)
    pen.forward(length / 2)

但是谁在全局设置sides?全局仅设置一次:

sides = []

sides的所有其他设置,例如。 mainGame()中的 local 。要使此工作有效,您需要在sides中声明mainGame()全局:

def mainGame():
    global sides

    userInput = ...

    ...

以及从函数内部设置全局sides的其他任何位置。总结:

  • 将全局sides设置为合理的默认int值(例如3

  • sides中声明mainGame()全局

  • 在(在线)Python教科书中了解global关键字

如果/当您了解dict时,应该可以使此代码更易于管理。像这样:

from turtle import Screen,Turtle

INSTRUCTION_FONT = ('Arial',12)
SHAPE_FONT = ('Arial',30,'bold')
TITLE_FONT = ('Avenir',18,'bold','underline')
VERSION_FONT = ('Arial',8)

def instructions():
    instPen = Turtle()
    instPen.hideturtle()
    instPen.penup()

    instPen.goto(0,205)
    instPen.write("For this game,you can:",align='center',font=INSTRUCTION_FONT)

    instPen.goto(-120,170)
    instPen.write("• Type in the name of a shape",font=INSTRUCTION_FONT)

    instPen.goto(-100,145)
    instPen.write("- Names must be in lowercase",120)
    instPen.write("- Shape must have 12 or less sides",95)
    instPen.write("• Type in a number of sides",70)
    instPen.write("- Number must be less than 25",35)
    instPen.write(": The program will then draw the shape",10)
    instPen.write(": And tell you what shape it is",font=INSTRUCTION_FONT)

    instPen.goto(-240,-340)
    instPen.write("Shape Shop v1.0",font=VERSION_FONT)

def regShapeDraw():
    length = 1 / sides * 500
    pen.forward(length / 2)

    angle = 360 / sides

    for _ in range(sides - 1):
        pen.left(angle)
        pen.forward(length)

    pen.left(angle)
    pen.forward(length / 2)

SHAPE_NAME_TO_SIDES = {
    'triangle': 3,'square': 4,'quadrilateral': 4,'pentagon': 5,'hexagon': 6,'septagon': 7,'heptagon': 7,'octagon': 8,'nonagon': 9,'decagon': 10,'hendecagon': 11,'dodecagon': 12,}

SHAPE_SIDES_TO_NAME = {
    '3': 'triangle','4': 'equare','5': 'pentagon','6': 'hexagon','7': 'heptagon','8': 'octagon','9': 'nonagon','10': 'decagon','11': 'hendecagon','12': 'dodecagon','13': 'triskaidecagon','14': 'tetrakaidecagon','15': 'pentadecagon','16': 'hexakaidecagon','17': 'heptadecagon','18': 'octakaidecagon','19': 'enneadecagon','20': 'icosagon','21': 'icosikaihenagon','22': 'icosikaidigon','23': 'icosikaitrigon','24': 'icositetragon','25': 'icosikaipentagon',}

SHAPE_NAME_ALIASES = {
    'quadrilateral': 'square','septagon': 'heptagon',}

def mainGame():
    global sides

    userInput = screen.textinput("What shape do you want? ","Enter a shape name or a number of sides:").lower()

    pen.clear()
    typeName.clear()

    if userInput.isalpha():
        if userInput in SHAPE_NAME_TO_SIDES:
            pen.penup()
            pen.goto(0,-300)
            pen.pendown()
            sides = SHAPE_NAME_TO_SIDES[userInput.lower()]
            regShapeDraw()
        else:
            pen.penup()
            pen.goto(0,-250)
            pen.write("We don't seem to have that shape in our database. You could try:",font=INSTRUCTION_FONT)
            pen.sety(-275)
            pen.write("» Making all the letters lowercase",font=INSTRUCTION_FONT)
            pen.sety(-300)
            pen.write("» Checking your spelling",font=INSTRUCTION_FONT)
            pen.sety(-325)
            pen.write("» Making sure the shape you want has 12 or less sides",font=INSTRUCTION_FONT)
    elif userInput.isnumeric():
        sides = int(userInput)

        if sides <= 25:
            pen.penup()
            pen.goto(0,-300)
            pen.pendown()
            regShapeDraw()
        else:
            pen.penup()
            pen.goto(0,font=INSTRUCTION_FONT)
            pen.sety(-275)
            pen.write("» Making the number less than 25",font=INSTRUCTION_FONT)
    else:
        pen.penup()
        pen.goto(0,-250)
        pen.write("We don't seem to have that shape in our database. You could try:",font=INSTRUCTION_FONT)
        pen.sety(-275)
        pen.write("» Making your input all letters or all numbers",font=INSTRUCTION_FONT)

    if userInput in SHAPE_SIDES_TO_NAME:
        shapeName = SHAPE_SIDES_TO_NAME[userInput]
    elif userInput in SHAPE_NAME_ALIASES:
        shapeName = SHAPE_NAME_ALIASES[userInput]
    elif userInput in SHAPE_NAME_TO_SIDES:
        shapeName = userInput
    else:
        shapeName = r"¯\(°_o)/¯"

    typeName.write(shapeName.title(),font=SHAPE_FONT)
    screen.listen()  # must be reasserted after screen.textinput()

screen = Screen()
screen.title("Shape Shop")
screen.setup(width=500,height=700)
screen.bgcolor('lightsteelblue')

turtle = Turtle()
turtle.hideturtle()
turtle.penup()
turtle.sety(250)
turtle.write("Welcome to the Shape Shop",font=TITLE_FONT)

instructions()

pen = Turtle()
pen.hideturtle()
pen.pensize(2)

typeName = Turtle()
typeName.hideturtle()
typeName.penup()
typeName.sety(-120)

sides = 3

screen.onkeypress(mainGame,'d')
screen.listen()
screen.mainloop()