如何在Python显示的某个部分中获取用户输入

2022-09-28 20:14:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要知道如何让Python项目在显示文本的中间而不是结尾要求用户输入。

示例:

name=input(("Whats your name? [ ]"))

我希望用户输入在[ ]之间 所以,它会让你在那里输入你的名字。我尝试移动(),因此代码看起来像这样:name=input("Whats your name?" ("[ ]")),但是我得到了以下错误:

TypeError: 'str' object is not callable

所以,如果你知道这个错误发生的原因,但不知道如何修复它,我会非常感谢关于这个错误的信息,如果你知道答案,我会非常感谢这两个。也许我需要导入一个模块。你知道吗


Tags: 项目代码用户name文本示例inputyour错误结尾名字我会让你在typeerrorwhats
1条回答
网友
1楼 ·

Blckknght

You could try appending several "\b" backspace characters to the end of your prompt string. On many (but perhaps not all) consoles those will make the input cursor move to the left. Your right bracket won't move with the input though, so you'll need to space it out enough to fit the largest name you expect to get. This is probably the only approach short of using curses or another console GUI library. That worked perfectly, thank you BlackNight!

热门问题