gpt4 book ai didi

python - 如何在 Python 3 中的输入后添加字符串?

转载 作者:行者123 更新时间:2023-12-01 06:37:02 25 4
gpt4 key购买 nike

我正在寻找用 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.

但我不明白如何在同一行的输入之后添加字符串或其他内容。我尝试使用 print("Something", end="") fUnction 但没有成功。

1  print('The cat do "Mi-', end="")
2 print('AOU!"')
3
4 print('I\'am thinking of : ', end="")
5 internalThought = input ("", end="")
6 print(".")

最佳答案

这在 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 是将光标向上移动一行的字符序列。

演示:

>>> 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.

在 Windows 上(不太好):

>>> 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.
>>>

chepner 关于探索 curses 的评论是正确的,如果您需要让它在 Windows 上运行。

我刚刚发现 Windows 支持 curses

说明

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

关于python - 如何在 Python 3 中的输入后添加字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59615115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com