简体   繁体   English

当我想说不止一件事时,如何在 Python 的 Turtle 图形中使用 textinput()?

[英]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.

相关问题 如果我将多个相同的事物添加到列表中,并且想要摆脱其中的一个,我怎么知道它是哪个? - If I append more than one of the same thing to a list and i want to get rid of one of them how do i know which one it is? 如何在python龟图形中更改龟的Hitbox大小? - How do I change the Hitbox size of a turtle in python turtle graphics? 如何使用带有海龟图形的Python制作线性图形计算器? - How do I make a linear graphing calculator in Python with turtle graphics? 如何使用 IDLE 在 python 海龟图形中插入图片? - How do i insert pictures in python turtle graphics using IDLE? Turtle graphics - 如何控制 window 何时关闭? - Turtle graphics - How do I control when the window closes? 如何两次使用python 3乌龟 - How do I use a python 3 turtle twice 如何在字典中使用多个标准 - How do I use more than one criteria in a dictionary 如何在 python 中的多个函数上使用相同的装饰器? - How do I use same decorators on more than one function in python? 当我运行代码时,Python Turtle Graphics 不断崩溃 - Python Turtle Graphics keeps crashing when I run the code 如何替换字符串中的多个字符 Python - How do I replace more than one character in a string Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM