变量中的Python-Prompt
我想知道如何在这本书上做一个练习:如何像计算机科学家一样思考:学习Python 3文档发行版第3版 这是我必须修改的代码:变量中的Python-Prompt,python,turtle-graphics,Python,Turtle Graphics,我想知道如何在这本书上做一个练习:如何像计算机科学家一样思考:学习Python 3文档发行版第3版 这是我必须修改的代码: import turtle window = turtle.Screen() window.bgcolor("lightgreen") # Set the window background color window.title("Hello, Tess!") # Set the window title tess = turtle.Turtle() tess.color(
import turtle
window = turtle.Screen()
window.bgcolor("lightgreen") # Set the window background color
window.title("Hello, Tess!") # Set the window title
tess = turtle.Turtle()
tess.color("blue") # Tell tess to change her color
tess.pensize(3) # Tell tess to set her pen width
tess.forward(50)
tess.left(120)
tess.forward(50)
window.mainloop()
这是请求:修改此程序,以便在创建窗口之前,提示用户输入所需的背景色。它应该存储用户的
响应一个变量,并根据用户的意愿修改窗口的颜色。执行类似的更改,以允许用户在运行时设置tess的颜色
我就是这样做的:
import turtle
window = turtle.Screen()
windowcolor = input ("window color: ")
tesscolor = input ("tess color: ")
window.bgcolor = windowcolor # Set the window background color
window.title("Hello, Tess!") # Set the window title
tess = turtle.Turtle()
tess.color = tesscolor # Tell tess to change her color
tess.pensize(3) # Tell tess to set her pen width
tess.forward(50)
tess.left(120)
tess.forward(50)
window.mainloop()
问题是屏幕和箭头根本没有改变颜色。。。
请帮帮我。谢谢
埃多尔多
TypeError:“str”对象不可调用您使用硬编码值在原始代码中调用window.bgcolor和tess.color,因此您还需要使用新值调用window.bgcolor和tess.color。现在您只是用这些值覆盖函数
所以像这样的事情
import turtle
windowcolor = input("window color: ")
tesscolor = input("tess color: ")
# Set up window
window = turtle.Screen()
window.bgcolor(windowcolor)
window.title("Hello, Tess!")
# Set up turtle
tess = turtle.Turtle()
tess.color(tesscolor)
tess.pensize(3)
# Draw things
tess.forward(50)
tess.left(120)
tess.forward(50)
# Enter window loop
window.mainloop()
请注意第一个代码window.bgcolorlightgreen与修改后的代码window.bgcolor=windowcolor之间的差异。苔丝当然也是这样。你是对的,但如果我尝试使用window.gbcolorvariableofcolor,我会在文章的末尾出现一个错误…删除引号:window.bgcolorwindowcolor。lightgreen本身是一个str值,而windowcolor是一个保存str值的变量。对不起,我的意思是window.gbcolorvariableofcolor,我有TypeError:“str”对象不可调用。如果使用gbcolor而不是gbcolor,则需要修复键入错误。如果您有str错误,您希望在使用建议的行之前将该实例变量分配给其他对象。我已经尝试按照您所说的做了,但我遇到了以下错误:File/home/edx/.config/spyder/turtlescreencolor.py,第17行,在window.bgcolorwindowcolor TypeError中:“str”对象不可用callable@EdoardoZucchelli,这个代码对我有用。也许您正在Python3下运行它,并在Python2中使用引号输入blue,而不是仅仅键入:blue