在Python 3中如何在输入后面添加字符串?

huangapple go评论81阅读模式
英文:

How to add a string right after an input in Python 3?

问题

1 打印("Francis - 你好陌生人!你叫什么名字?")
2 name = 输入("??? - 我叫")
3 打印(f"Francis - 哦,你好{name}。你需要去南方。")

在这里,我只会写我的名字"Arnaud",我希望程序会自动在第2行(在我的输入后)添加句子("and I need to go in town.")。

应该是这样的:

1 Francis - 你好陌生人!你叫什么名字?
2 ??? - 我叫 Arnaud,我需要去城里。# 在这里,我只会写我的名字"Arnaud"
3 Francis - 哦,你好Arnaud。你需要去南方。

但是我不知道如何在输入后立即添加字符串或其他内容。我尝试使用print("Something", end="")函数,但没有成功。

1 打印('猫在唱“Mi-', end="")
2 打印('AOU!"')
3
4 打印('我在想:', end="")
5 internalThought = 输入("", end="")
6 打印("。")

英文:

I'm looking to create a little game in Python 3 where, when you answer the question with an input, the code automatically adds something right after you on the same line :

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

So here, I'll just write my name "Arnaud" and I would like that the program adds automatically the sentence ("and I need to go in town.") right after "Arnaud" in line 2 (after my input).

It should looks like:

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.

But I don't understand how to add an string or else right after an input on the same line. I tried with the print("Something", end="") fUnction but didn't work.

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

答案1

得分: 1

这部分内容的中文翻译如下:

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

print("Francis - 陌生人你好!你叫什么名字?")
name = input("??? - 我的名字是")
print(f"3[A??? - 我的名字是 ***{name}***,我需要去镇里。")
print(f"Francis - 哦,你好{name}。你需要往南走。")

\033[A是移动光标上移一行的字符序列。

演示:

def foo():
    print("Francis - 陌生人你好!你叫什么名字?")
    name = input("??? - 我的名字是")
    print(f"3[A??? - 我的名字是 ***{name}***,我需要去镇里。")
    print(f"Francis - 哦,你好{name}。你需要往南走。")

foo()

在Windows上(效果不佳):

def foo():
    print("Francis - 陌生人你好!你叫什么名字?")
    name = input("??? - 我的名字是")
    print(f"3[A??? - 我的名字是 ***{name}***,我需要去镇里。")
    print(f"Francis - 哦,你好{name}。你需要往南走。")

foo()

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

我刚刚发现curses在Windows下是支持的。

解释

\033[A写入输出会将光标移到上一个输入语句执行的地方,而不是计算如何移动到??? - 我的名字是的末尾以写入剩下的内容(即 ***{name} and I need to go in town.),我们只需重写??? - 我的名字是,这样就可以达到与跳过该字符串相同的效果。

英文:

This won't work in Windows, but should work on Linux/Unix.

print("Francis - Hello Stranger ! What's your name ?")
name = input("??? - My name is ")
print(f"3[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 is the character sequence to move the cursor up one line.

Demo:

>>> def foo():
...     print("Francis - Hello Stranger ! What's your name ?")
...     name = input("??? - My name is ")
...     print(f"3[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.

On Windows (not so good):

>>> def foo():
...     print("Francis - Hello Stranger ! What's your name ?")
...     name = input("??? - My name is ")
...     print(f"3[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.
>>>

<strike>chepner's comment about exploring curses is on point if you need to get this to work on Windows.</strike>

I just discovered that curses is not supported under Windows.

Explanation

Writing \033[A to the output moves the cursor up one line back to where the input statement was executed. Rather that figure how to now space over to the end of ??? - My name is to write the rest of what needs to be written (that is, ***{name} and I need to go in town.), we just rewrite ??? - My name is , which has the same effect as spacing over that string.

答案2

得分: 0

"Hey you can do this way !
print (&quot; ?? - My name is {0} and I need to go in town &quot; .format(input()))"

(你可以这样做!
print(“ ?? - 我的名字是{0},我需要去城里” .format(input()))

英文:

Hey you can do this way !
print (&quot; ?? - My name is {0} and I need to go in town &quot; .format(input()))

答案3

得分: 0

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

英文:

You cannot print a string immediately after the input() function on the same line in python3. There are complex workarounds to build custom solutions, but you would need to rewrite the source code for the input() function, therefore not using the input() function.

huangapple
  • 本文由 发表于 2020年1月6日 23:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定