【问题标题】:Problems prompting user for input in Python提示用户在 Python 中输入的问题
【发布时间】:2017-11-22 04:34:28
【问题描述】:
import sys
import turtle
t=turtle.Pen
def what_to_draw():
    print ("What to do you want to see a sketch of that may or may not be colored?")
what_to_draw=sys.stdin.readline()
if what_to_draw=="flower/n":
    t.forward(90)
elif():
    print ("What you typed isn't in the code. Try putting a letter or letters to lowercase or uppercase. If that doesn't work, what you typed has not been set to make something happen")

我在上面输入了这段代码。它在python shell中说“花”没有定义。谁能帮我解决这个问题?

【问题讨论】:

    标签: python python-3.x turtle-graphics


    【解决方案1】:

    你的代码有几行有某种错误:

    t=turtle.Pen 应该是:t = turtle.Pen()

    你应该避免使用同名的函数和变量

    def what_to_draw():
        ...
        what_to_draw = sys.stdin.readline()
    

    使用rstrip()处理"\n",使用.lower()处理大小写:

    if what_to_draw == "flower/n":
    

    elif(): 需要某种条件,否则使用else:

    让我们尝试不同的方法。让我们尝试使用 Python 3 turtle 新增的 textinput() 函数在海龟内部完成所有操作,而不是将控制台窗口输入与海龟图形混合:

    from turtle import Turtle, Screen
    
    def what_to_draw():
        title = "Make a sketch."
    
        while True:
            to_draw = screen.textinput(title, "What do you want to see?")
    
            if to_draw is None:  # user hit Cancel so quit
                break
    
            to_draw = to_draw.strip().lower()  # clean up input
    
            if to_draw == "flower":
                tortoise.forward(90)  # draw a flower here
                break
            elif to_draw == "frog":
                tortoise.backward(90)  # draw a frog here
                break
            else:
                title = to_draw.capitalize() + " isn't in the code."
    
    tortoise = Turtle()
    
    screen = Screen()
    
    what_to_draw()
    
    screen.mainloop()
    

    【讨论】:

    • @Winston,我稍微重新排列了我的示例,以便更轻松地添加新的绘图选项。至于提示的内容,这取决于您 - 只需检查您的消息是否适合相对较小的文本输入框。
    • 还有可以把输入框做大吗?
    • 我稍微改了代码,但是当我复制粘贴新代码时,它不会让我添加注释
    【解决方案2】:

    你的缩进是错误的,所以大部分语句都在what_to_draw()的函数体之外。
    您实际上并没有调用该函数,因此它不会做任何事情。
    另外,不要将what_to_draw 用作函数名和变量名。
    elif:之后不应该有()
    使用 input() 代替 print()stdin。你也不会这样得到\n

    试试这个并告诉我:

    import sys
    import turtle
    t=turtle.Pen()
    def what_to_draw():
        draw_this = input("What to do you want to see a sketch of that may or may not be colored?")
        if draw_this == "flower":
            t.forward(90)
        elif:
            print ("What you typed isn't in the code. Try putting a letter or letters to lowercase or uppercase. If that doesn't work, what you typed has not been set to make something happen")
    
    what_to_draw()
    

    【讨论】:

    • 这是一个不同的问题。如果它不起作用,那么现在您的代码中存在逻辑问题。您应该处理它并尝试找出您的命令缺少什么。如问题中所述,您的代码根本无法运行的原因已得到解决。您应该将此标记为已回答并尝试自己解决。
    • 不,如果当前的材料不适合您,请从不同的来源重新研究材料。去谷歌上查询。你会在编程中遇到很多这样的减速。计算机完全按照您的指示行事,不多不少。
    • 我没有强迫你。德古拉就是这样做的。您的问题已得到解答。现在你面临一个不同的问题。问题是“为什么它没有定义花”。这已经得到了帮助,而且远不止于此。
    • 接受答案是对那些花时间帮助陌生人的人的感谢。你告诉乌龟向前移动 90 像素,它确实做到了。它不会从那个声明中得出一朵花。另外,您是否按照这里的建议查看了 turtle.mainloop() ?
    • 此评论线程变得笨拙且对未来的读者没有帮助。打开一个新问题并发布不起作用的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多