在Python3中,如何在输入之后添加字符串?

在Python3中,如何在输入之后添加字符串?,python,python-3.x,input,Python,Python 3.x,Input,我想在Python 3中创建一个小游戏,当您用输入回答问题时,代码会自动在您的后面添加一些内容: 1 print("Francis - Hello Stranger ! What's your name ?") 2 name = input("??? - My name is ") 3 print(f"Francis - Oh, hello {name}. You need to go South.") 所以在这里,我只写我的名字“Arnaud”,我希望程序在第2行“Arnaud”之后(

我想在Python 3中创建一个小游戏,当您用输入回答问题时,代码会自动在您的后面添加一些内容:

1  print("Francis - Hello Stranger ! What's your name ?")
2  name = input("??? - My name is ")
3  print(f"Francis - Oh, hello {name}. You need to go South.")
所以在这里,我只写我的名字“Arnaud”,我希望程序在第2行“Arnaud”之后(在我输入之后)自动添加句子(“我需要进城”)

应该是这样的:

1  Francis - Hello Stranger ! What's your name ?
2  ??? - My name is ***Arnaud*** and I need to go in town. #Here, I just write my name "Arnaud"
3  Francis - Oh, hello Arnaud. You need to go South.
但我不明白如何在同一行的输入之后添加字符串或其他内容。我尝试了打印(“某物”,end=“”)功能,但没有成功

1  print('The cat do "Mi-', end="")
2  print('AOU!"')
3  
4  print('I\'am thinking of : ', end="")
5  internalThought = input ("", end="")
6  print(".")
嘿,你可以这样做!
print(“??-我的名字是{0},我需要进城去”。format(input())

这在Windows中不起作用,但在Linux/Unix上应该可以

print("Francis - Hello Stranger ! What's your name ?")
name = input("??? - My name is ")
print(f"\033[A??? - My name is ***{name}*** and I need to go in town.")
print(f"Francis - Oh, hello {name}. You need to go South.")
\033[A
是将光标上移一行的字符序列

演示:

在Windows上(不太好):

如果您需要在Windows上使用它,chepner关于探索
诅咒
的评论非常贴切

我刚刚发现Windows下不支持
诅咒

解释


\033[A
写入输出会将光标向上移动一行,返回执行
输入语句的位置。相反,该图显示了现在如何将空格移到
??-我的名字是
来写其余需要写入的内容(即,
***{name},我需要进城。
),我们只是重写了
?-我的名字是
,它与在该字符串上的间距具有相同的效果。

在python3中,不能在同一行的input()函数之后立即打印字符串。构建自定义解决方案有复杂的变通方法,但需要重写input()的源代码函数,因此不使用输入()函数。

input
是一个相当粗糙的工具:它将字符串写入标准输出,然后从标准输入中读取。为了满足您的需要,您需要类似于
诅咒的东西。
。不幸的是,这不允许在用户输入前的同一行上提示某些内容,在该行中,句子将被写入answer但是正如Romain所说,当我写“Arnaud”时,代码没有提示???-我的名字是“在输入之前”。另外,代码没有将它存储在变量“name”中.所以最后,玩家将不得不重写他/她的名字。=/我还没有达到那个级别!谢谢你的回答,但我不理解你的代码…只有在linux上说我可以工作的部分,因为有一个命令可以将光标向上移动一行(但即使如此)无论如何,谢谢!我已经更新了答案,试图解释代码的作用。
>>> def foo():
...     print("Francis - Hello Stranger ! What's your name ?")
...     name = input("??? - My name is ")
...     print(f"\033[A??? - My name is ***{name}*** and I need to go in town.")
...     print(f"Francis - Oh, hello {name}. You need to go South.")
...
>>> foo()
Francis - Hello Stranger ! What's your name ?
??? - My name is ***Arnaud*** and I need to go in town.
Francis - Oh, hello Arnaud. You need to go South.
>>> def foo():
...     print("Francis - Hello Stranger ! What's your name ?")
...     name = input("??? - My name is ")
...     print(f"\033[A??? - My name is ***{name}*** and I need to go in town.")
...     print(f"Francis - Oh, hello {name}. You need to go South.")
...
>>> foo()
Francis - Hello Stranger ! What's your name ?
??? - My name is Arnaud
←[A??? - My name is ***Arnaud*** and I need to go in town.
Francis - Oh, hello Arnaud. You need to go South.
>>>