如何使print()接受同一行中的用户输入?

2024-05-21 03:22:48 发布

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

当我在python中获取用户输入时,它在下一行中获取输入,但我希望ti在同一行中获取输入。如何做到这一点?你知道吗

我是这样接受意见的

print("Enter your name:",end=" ")

它在控制台上显示为

Enter your name:
Ankit

但我想把它当作

Enter your name:Ankit

Tags: 用户nameyourtiend意见printenter
3条回答

如果您正在使用Python 2.x

response = raw_input("Enter your name:")

如果您正在使用Python 3.x

response = input("Enter your name:")

替代解决方案:

对于python 2.x:

print("Enter your name:"),
response = raw_input()

对于python 3.x:

print("Enter your name:", end="")
response = input()

通过使用input()方法 只是打字

userinput = input("Enter your name: ")

您需要使用input方法:

response = input("Enter your name:")

(或者raw_input对于python2)

相关问题 更多 >