提示用户在Python中输入的问题

时间:2017-11-22 04:34:29

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

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中“flower”没有定义。有人能为我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

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

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龟的新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()

答案 1 :(得分:0)

您的缩进是错误的,因此大部分陈述都在what_to_draw()的函数体之外 你实际上并没有打电话给这个功能,所以它不会做任何事情 另外,请勿将what_to_draw用作函数名称和变量名称 在()之后不应该elif: 使用print()代替stdininput()。你也不能以\n的方式获得。{/ p>

试试这个让我知道:

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