[英]How do I use textinput() in Turtle graphics for Python when I want to say more than one thing?
I would like to use textinput()
to solicit from the user an x, y coordinate.我想使用
textinput()
向用户征求 x、y 坐标。 I look at what they've entered and if it's not valid input, I ask them for more input.我查看他们输入的内容,如果输入无效,我会要求他们提供更多输入。
so let's say xy = 3,2
and that isn't valid.所以让我们说
xy = 3,2
并且这是无效的。 I want to be able to say:我希望能够说:
turtle.textinput("", xy, "is not a valid input. Enter new x,y")
However, this offers too many arguments but if I do something like the following, it puts {} around each aspect.但是,这提供了太多参数,但如果我执行以下操作,它会将 {} 放在每个方面。
turtle.textinput("", (ui, "is not a valid input. Enter new x,y"))
Is there a ways to just get it to prompt nicely: "3,2 is not a valid input. Enter new x,y"?有没有办法让它很好地提示:“3,2 不是一个有效的输入。输入新的 x,y”? I haven't found much literature online.
我在网上找到的文献不多。
So I actually kept playing and figured this out.所以我实际上一直在玩,并弄清楚了这一点。 I figured I'd leave the question up in case it helps someone else in the future.
我想我会留下这个问题,以防将来对其他人有帮助。
Using formatted string liberals is the key!使用格式化的字符串自由是关键! I converted the pair to ints instead of strings within the list, then when I used textinput, instead of just having the second argument be a string, it was:
我将这对转换为整数而不是列表中的字符串,然后当我使用 textinput 时,而不是仅仅让第二个参数是一个字符串,它是:
turtle.textinput("", f"{xy} is not a valid input. Enter new x,y") turtle.textinput("", f"{xy} 不是有效的输入。输入新的 x,y")
Voila!瞧! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.