代码 returns 不支持的操作数的回溯错误。看不出问题
Code returns Traceback Error for unsupported operand. Cannot see the problem
目前正在为学校做作业。我的代码在几行代码中返回错误,并建议我使用 Tkinter,但我没有。请寻找任何有时间关注我的代码的人。
为了能够看到出现的错误,我按“d”打开输入 window,然后输入“square”——全部小写。
我正在尝试让程序在您输入形状名称或它的边数时绘制形状。
我的错误返回为:
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 200820 no comments Stack.py", line 85, in startUp
mainGame()
File "E:\Master 200820 no comments Stack.py", line 125, in mainGame
regShapeDraw()
File "E:\Master 200820 no comments Stack.py", line 73, in regShapeDraw
length = ((1 / sides) * 500)
TypeError: unsupported operand type(s) for /: 'int' and 'list'
这是我的代码:
import turtle
import math
import random
import time
screen = turtle.Screen()
screen.title("Shape Shop")
screen.setup(width=500, height=700)
screen.bgcolor('lightsteelblue')
turtleS = turtle.Turtle()
turtleS.ht()
turtleS.penup()
turtleS.goto(0, 250)
turtleS.write("Welcome to the Shape Shop", align="center", font=("Avenir", 18, "bold", "underline"))
pen = turtle.Turtle()
pen.pensize(2)
pen.ht()
def instructions():
instPen = turtle.Turtle()
instPen.ht()
instPen.penup()
instPen.goto(0, 205)
instPen.pendown()
instPen.write("For this game, you can:", align="center", font=("Avenir", 12))
instPen.penup()
instPen.goto(-120, 170)
instPen.pendown()
instPen.write("• Type in the name of a shape", font=("Avenir", 12))
instPen.penup()
instPen.goto(-100, 145)
instPen.pendown()
instPen.write("- Names must be in lowercase", font=("Avenir", 12))
instPen.penup()
instPen.goto(-100, 120)
instPen.pendown()
instPen.write("- Shape must have 12 or less sides", font=("Avenir", 12))
instPen.penup()
instPen.goto(-120, 95)
instPen.pendown()
instPen.write("• Type in a number of sides", font=("Avenir", 12))
instPen.penup()
instPen.goto(-100, 70)
instPen.pendown()
instPen.write("- Number must be less than 25", font=("Avenir", 12))
instPen.penup()
instPen.goto(-120, 35)
instPen.pendown()
instPen.write(": The program will then draw the shape", font=("Avenir", 12))
instPen.penup()
instPen.goto(-120, 10)
instPen.pendown()
instPen.write(": And tell you what shape it is", font=("Avenir", 12))
instPen.penup()
instPen.goto(-240, -340)
instPen.pendown()
instPen.write("Shape Shop v1.0", font=("Avenir", 8))
instructions()
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 = []
def startUp():
mainGame()
screen.listen()
screen.onkeypress(startUp, "d")
def mainGame():
userInput = screen.textinput("What shape do you want? ", "Enter a shape name or a number of sides: ")
if userInput == "COOL":
eggPen = turtle.Turtle()
eggPen.speed(0)
eggPen.color('black')
eggPen.penup()
eggPen.goto(0, -80)
eggPen.pendown()
rotate=int(360)
def drawCircles(t,size):
size = 70
for i in range(5):
t.circle(size)
size=size-4
def drawSpecial(t,size,repeat):
for i in range (repeat):
drawCircles(t,size)
t.right(360/repeat)
drawSpecial(eggPen,100,10)
shapeName = "WOAH!"
else:
if userInput.isalpha():
if userInput == "triangle":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 3
regShapeDraw()
elif userInput == "square":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 4
regShapeDraw()
elif userInput == "quadrilateral":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 4
regShapeDraw()
elif userInput == "pentagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 5
regShapeDraw()
elif userInput == "hexagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 6
regShapeDraw()
elif userInput == "septagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 7
regShapeDraw()
elif userInput == "heptagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 7
regShapeDraw()
elif userInput == "octagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 8
regShapeDraw()
elif userInput == "nonagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 9
regShapeDraw()
elif userInput == "decagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 10
regShapeDraw()
elif userInput == "hendecagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 11
regShapeDraw()
elif userInput == "dodecagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 12
regShapeDraw()
else:
pen.penup()
pen.goto(0, -250)
pen.pendown()
pen.write("We don't seem to have that shape in our database. You could try:", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -275)
pen.pendown()
pen.write("» Making all the letters lowercase", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -300)
pen.pendown()
pen.write("» Checking your spelling", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -325)
pen.pendown()
pen.write("» Making sure the shape you want has 12 or less sides", align="center", font=("Arial", 12))
elif userInput.isnumeric():
if int(userInput) <= 25:
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = int(userInput)
regShapeDraw()
else:
pen.penup()
pen.goto(0, -250)
pen.pendown()
pen.write("We don't seem to have that shape in our database. You could try:", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -275)
pen.pendown()
pen.write("» Making the number less than 25", align="center", font=("Arial", 12))
else:
pen.penup()
pen.goto(0, -250)
pen.pendown()
pen.write("We don't seem to have that shape in our database. You could try:", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -275)
pen.pendown()
pen.write("» Making your input all letters or all numbers", align="center", font=("Arial", 12))
if userInput == "3":
shapeName = "Triangle"
elif userInput == "4":
shapeName = "Square"
elif userInput == "5":
shapeName = "Pentagon"
elif userInput == "6":
shapeName = "Hexagon"
elif userInput == "7":
shapeName = "Heptagon"
elif userInput == "8":
shapeName = "Octagon"
elif userInput == "9":
shapeName = "Nonagon"
elif userInput == "10":
shapeName = "Decagon"
elif userInput == "11":
shapeName = "Hendecagon"
elif userInput == "12":
shapeName = "Dodecagon"
elif userInput == "13":
shapeName = "Triskaidecagon"
elif userInput == "14":
shapeName = "Tetrakaidecagon"
elif userInput == "15":
shapeName = "Pentadecagon"
elif userInput == "16":
shapeName = "Hexakaidecagon"
elif userInput == "17":
shapeName = "Heptadecagon"
elif userInput == "18":
shapeName = "Octakaidecagon"
elif userInput == "19":
shapeName = "Enneadecagon"
elif userInput == "20":
shapeName = "Icosagon"
elif userInput == "21":
shapeName = "Icosikaihenagon"
elif userInput == "22":
shapeName = "Icosikaidigon"
elif userInput == "23":
shapeName = "Icosikaitrigon"
elif userInput == "24":
shapeName = "Icositetragon"
elif userInput == "25":
shapeName = "Icosikaipentagon"
elif userInput == "triangle":
shapeName = "Triangle"
elif userInput == "square":
shapeName = "Square"
elif userInput == "quadrilateral":
shapeName = "Square"
elif userInput == "pentagon":
shapeName = "Pentagon"
elif userInput == "hexagon":
shapeName = "Hexagon"
elif userInput == "heptagon":
shapeName = "Heptagon"
elif userInput == "septagon":
shapeName = "Heptagon"
elif userInput == "octagon":
shapeName = "Octagon"
elif userInput == "nonagon":
shapeName = "Nonagon"
elif userInput == "decagon":
shapeName = "Decagon"
elif userInput == "hendecagon":
shapeName = "Hendecagon"
elif userInput == "dodecagon":
shapeName = "Dodecagon"
else:
shapeName = "¯\(°_o)/¯"
typeName = turtle.Turtle()
typeName.ht()
typeName.penup()
typeName.goto(0, -120)
typeName.pendown()
typeName.write(shapeName, align="center", font=("Arial", 30, "bold"))
screen.mainloop()
My code is coming back with errors in a couple of lines of code, and
suggests I'm using Tkinter, which I have not.
如果您使用的是 turtle,那么您使用的是 tkinter,因为 turtle 位于 tkinter 之上。至于错误:
File "E:\Master 200820 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。要使这项工作正常进行,您需要在 mainGame()
:
中声明 sides
全局
def mainGame():
global sides
userInput = ...
...
以及其他任何地方,全局 sides
是从一个函数中设置的。总结:
将全局 sides
设置为合理的默认 int
值(例如 3
)
在 mainGame()
中声明 sides
全局
阅读您的(在线)Python 教科书中的 global
关键字
If/when 你了解了 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", font=INSTRUCTION_FONT)
instPen.goto(-100, 120)
instPen.write("- Shape must have 12 or less sides", font=INSTRUCTION_FONT)
instPen.goto(-120, 95)
instPen.write("• Type in a number of sides", font=INSTRUCTION_FONT)
instPen.goto(-100, 70)
instPen.write("- Number must be less than 25", font=INSTRUCTION_FONT)
instPen.goto(-120, 35)
instPen.write(": The program will then draw the shape", font=INSTRUCTION_FONT)
instPen.goto(-120, 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:", align='center', font=INSTRUCTION_FONT)
pen.sety(-275)
pen.write("» Making all the letters lowercase", align='center', font=INSTRUCTION_FONT)
pen.sety(-300)
pen.write("» Checking your spelling", align='center', font=INSTRUCTION_FONT)
pen.sety(-325)
pen.write("» Making sure the shape you want has 12 or less sides", align='center', 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, -250)
pen.write("We don't seem to have that shape in our database. You could try:", align='center', font=INSTRUCTION_FONT)
pen.sety(-275)
pen.write("» Making the number less than 25", align='center', 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:", align='center', font=INSTRUCTION_FONT)
pen.sety(-275)
pen.write("» Making your input all letters or all numbers", align='center', 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(), align='center', 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", align='center', 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()
目前正在为学校做作业。我的代码在几行代码中返回错误,并建议我使用 Tkinter,但我没有。请寻找任何有时间关注我的代码的人。
为了能够看到出现的错误,我按“d”打开输入 window,然后输入“square”——全部小写。
我正在尝试让程序在您输入形状名称或它的边数时绘制形状。
我的错误返回为:
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 200820 no comments Stack.py", line 85, in startUp
mainGame()
File "E:\Master 200820 no comments Stack.py", line 125, in mainGame
regShapeDraw()
File "E:\Master 200820 no comments Stack.py", line 73, in regShapeDraw
length = ((1 / sides) * 500)
TypeError: unsupported operand type(s) for /: 'int' and 'list'
这是我的代码:
import turtle
import math
import random
import time
screen = turtle.Screen()
screen.title("Shape Shop")
screen.setup(width=500, height=700)
screen.bgcolor('lightsteelblue')
turtleS = turtle.Turtle()
turtleS.ht()
turtleS.penup()
turtleS.goto(0, 250)
turtleS.write("Welcome to the Shape Shop", align="center", font=("Avenir", 18, "bold", "underline"))
pen = turtle.Turtle()
pen.pensize(2)
pen.ht()
def instructions():
instPen = turtle.Turtle()
instPen.ht()
instPen.penup()
instPen.goto(0, 205)
instPen.pendown()
instPen.write("For this game, you can:", align="center", font=("Avenir", 12))
instPen.penup()
instPen.goto(-120, 170)
instPen.pendown()
instPen.write("• Type in the name of a shape", font=("Avenir", 12))
instPen.penup()
instPen.goto(-100, 145)
instPen.pendown()
instPen.write("- Names must be in lowercase", font=("Avenir", 12))
instPen.penup()
instPen.goto(-100, 120)
instPen.pendown()
instPen.write("- Shape must have 12 or less sides", font=("Avenir", 12))
instPen.penup()
instPen.goto(-120, 95)
instPen.pendown()
instPen.write("• Type in a number of sides", font=("Avenir", 12))
instPen.penup()
instPen.goto(-100, 70)
instPen.pendown()
instPen.write("- Number must be less than 25", font=("Avenir", 12))
instPen.penup()
instPen.goto(-120, 35)
instPen.pendown()
instPen.write(": The program will then draw the shape", font=("Avenir", 12))
instPen.penup()
instPen.goto(-120, 10)
instPen.pendown()
instPen.write(": And tell you what shape it is", font=("Avenir", 12))
instPen.penup()
instPen.goto(-240, -340)
instPen.pendown()
instPen.write("Shape Shop v1.0", font=("Avenir", 8))
instructions()
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 = []
def startUp():
mainGame()
screen.listen()
screen.onkeypress(startUp, "d")
def mainGame():
userInput = screen.textinput("What shape do you want? ", "Enter a shape name or a number of sides: ")
if userInput == "COOL":
eggPen = turtle.Turtle()
eggPen.speed(0)
eggPen.color('black')
eggPen.penup()
eggPen.goto(0, -80)
eggPen.pendown()
rotate=int(360)
def drawCircles(t,size):
size = 70
for i in range(5):
t.circle(size)
size=size-4
def drawSpecial(t,size,repeat):
for i in range (repeat):
drawCircles(t,size)
t.right(360/repeat)
drawSpecial(eggPen,100,10)
shapeName = "WOAH!"
else:
if userInput.isalpha():
if userInput == "triangle":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 3
regShapeDraw()
elif userInput == "square":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 4
regShapeDraw()
elif userInput == "quadrilateral":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 4
regShapeDraw()
elif userInput == "pentagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 5
regShapeDraw()
elif userInput == "hexagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 6
regShapeDraw()
elif userInput == "septagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 7
regShapeDraw()
elif userInput == "heptagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 7
regShapeDraw()
elif userInput == "octagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 8
regShapeDraw()
elif userInput == "nonagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 9
regShapeDraw()
elif userInput == "decagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 10
regShapeDraw()
elif userInput == "hendecagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 11
regShapeDraw()
elif userInput == "dodecagon":
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = 12
regShapeDraw()
else:
pen.penup()
pen.goto(0, -250)
pen.pendown()
pen.write("We don't seem to have that shape in our database. You could try:", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -275)
pen.pendown()
pen.write("» Making all the letters lowercase", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -300)
pen.pendown()
pen.write("» Checking your spelling", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -325)
pen.pendown()
pen.write("» Making sure the shape you want has 12 or less sides", align="center", font=("Arial", 12))
elif userInput.isnumeric():
if int(userInput) <= 25:
pen.penup()
pen.goto(0, -300)
pen.pendown()
sides = int(userInput)
regShapeDraw()
else:
pen.penup()
pen.goto(0, -250)
pen.pendown()
pen.write("We don't seem to have that shape in our database. You could try:", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -275)
pen.pendown()
pen.write("» Making the number less than 25", align="center", font=("Arial", 12))
else:
pen.penup()
pen.goto(0, -250)
pen.pendown()
pen.write("We don't seem to have that shape in our database. You could try:", align="center", font=("Arial", 12))
pen.penup()
pen.goto(0, -275)
pen.pendown()
pen.write("» Making your input all letters or all numbers", align="center", font=("Arial", 12))
if userInput == "3":
shapeName = "Triangle"
elif userInput == "4":
shapeName = "Square"
elif userInput == "5":
shapeName = "Pentagon"
elif userInput == "6":
shapeName = "Hexagon"
elif userInput == "7":
shapeName = "Heptagon"
elif userInput == "8":
shapeName = "Octagon"
elif userInput == "9":
shapeName = "Nonagon"
elif userInput == "10":
shapeName = "Decagon"
elif userInput == "11":
shapeName = "Hendecagon"
elif userInput == "12":
shapeName = "Dodecagon"
elif userInput == "13":
shapeName = "Triskaidecagon"
elif userInput == "14":
shapeName = "Tetrakaidecagon"
elif userInput == "15":
shapeName = "Pentadecagon"
elif userInput == "16":
shapeName = "Hexakaidecagon"
elif userInput == "17":
shapeName = "Heptadecagon"
elif userInput == "18":
shapeName = "Octakaidecagon"
elif userInput == "19":
shapeName = "Enneadecagon"
elif userInput == "20":
shapeName = "Icosagon"
elif userInput == "21":
shapeName = "Icosikaihenagon"
elif userInput == "22":
shapeName = "Icosikaidigon"
elif userInput == "23":
shapeName = "Icosikaitrigon"
elif userInput == "24":
shapeName = "Icositetragon"
elif userInput == "25":
shapeName = "Icosikaipentagon"
elif userInput == "triangle":
shapeName = "Triangle"
elif userInput == "square":
shapeName = "Square"
elif userInput == "quadrilateral":
shapeName = "Square"
elif userInput == "pentagon":
shapeName = "Pentagon"
elif userInput == "hexagon":
shapeName = "Hexagon"
elif userInput == "heptagon":
shapeName = "Heptagon"
elif userInput == "septagon":
shapeName = "Heptagon"
elif userInput == "octagon":
shapeName = "Octagon"
elif userInput == "nonagon":
shapeName = "Nonagon"
elif userInput == "decagon":
shapeName = "Decagon"
elif userInput == "hendecagon":
shapeName = "Hendecagon"
elif userInput == "dodecagon":
shapeName = "Dodecagon"
else:
shapeName = "¯\(°_o)/¯"
typeName = turtle.Turtle()
typeName.ht()
typeName.penup()
typeName.goto(0, -120)
typeName.pendown()
typeName.write(shapeName, align="center", font=("Arial", 30, "bold"))
screen.mainloop()
My code is coming back with errors in a couple of lines of code, and suggests I'm using Tkinter, which I have not.
如果您使用的是 turtle,那么您使用的是 tkinter,因为 turtle 位于 tkinter 之上。至于错误:
File "E:\Master 200820 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。要使这项工作正常进行,您需要在 mainGame()
:
sides
全局
def mainGame():
global sides
userInput = ...
...
以及其他任何地方,全局 sides
是从一个函数中设置的。总结:
将全局
sides
设置为合理的默认int
值(例如3
)在
中声明mainGame()
sides
全局阅读您的(在线)Python 教科书中的
global
关键字
If/when 你了解了 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", font=INSTRUCTION_FONT)
instPen.goto(-100, 120)
instPen.write("- Shape must have 12 or less sides", font=INSTRUCTION_FONT)
instPen.goto(-120, 95)
instPen.write("• Type in a number of sides", font=INSTRUCTION_FONT)
instPen.goto(-100, 70)
instPen.write("- Number must be less than 25", font=INSTRUCTION_FONT)
instPen.goto(-120, 35)
instPen.write(": The program will then draw the shape", font=INSTRUCTION_FONT)
instPen.goto(-120, 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:", align='center', font=INSTRUCTION_FONT)
pen.sety(-275)
pen.write("» Making all the letters lowercase", align='center', font=INSTRUCTION_FONT)
pen.sety(-300)
pen.write("» Checking your spelling", align='center', font=INSTRUCTION_FONT)
pen.sety(-325)
pen.write("» Making sure the shape you want has 12 or less sides", align='center', 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, -250)
pen.write("We don't seem to have that shape in our database. You could try:", align='center', font=INSTRUCTION_FONT)
pen.sety(-275)
pen.write("» Making the number less than 25", align='center', 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:", align='center', font=INSTRUCTION_FONT)
pen.sety(-275)
pen.write("» Making your input all letters or all numbers", align='center', 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(), align='center', 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", align='center', 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()