Python 巨蟒:“;“打印”;及;输入“;一行
如果我想在python中的文本之间输入一些内容,那么在用户输入内容并按下enter键后,如果不切换到新行,我怎么做呢 例如: 应修改为在一行中输出到控制台,说明:Python 巨蟒:“;“打印”;及;输入“;一行,python,input,printing,line,python2.x,python3.x,Python,Input,Printing,Line,Python2.x,Python3.x,如果我想在python中的文本之间输入一些内容,那么在用户输入内容并按下enter键后,如果不切换到新行,我怎么做呢 例如: 应修改为在一行中输出到控制台,说明: I have h apples and h1 pears. 它应该在一行上这一事实没有更深层的意义,它是假设性的,我希望它看起来像这样。您可以做以下几点: print 'I have %s apples and %s pears.'%(input(),input()) 基本上你有一个字符串,你有两个输入的共振峰 编辑: 据我所知,
I have h apples and h1 pears.
它应该在一行上这一事实没有更深层的意义,它是假设性的,我希望它看起来像这样。您可以做以下几点:
print 'I have %s apples and %s pears.'%(input(),input())
基本上你有一个字符串,你有两个输入的共振峰
编辑:
据我所知,用两个输入在一条线上实现所有功能是不容易的。你能得到的最接近的是:
print 'I have',
a=input()
print 'apples and',
p=input()
print 'pears.'
这将输出:
I have 23
apples and 42
pears.
逗号表示法防止在print语句后出现新行,但输入后的返回仍然存在。虽然另一个答案正确,但不推荐使用
%
,而应使用string.format()
方法。这是你可以做的
print "I have {0} apples and {1} pears".format(raw_input(), raw_input())
另外,从你的问题来看,不清楚你是否在使用或,所以这里也有一个答案
print("I have {0} apples and {1} pears".format(input(), input()))
如果我理解正确的话,您要做的是在不重复换行符的情况下获取输入。如果您使用的是Windows,则可以使用msvcrt模块的getwch方法获取输入的单个字符,而不打印任何内容(包括换行符),如果该字符不是换行符,则打印该字符。否则,您需要定义一个getch函数:
import sys
try:
from msvcrt import getwch as getch
except ImportError:
def getch():
"""Stolen from http://code.activestate.com/recipes/134892/"""
import tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def input_():
"""Print and return input without echoing newline."""
response = ""
while True:
c = getch()
if c == "\b" and len(response) > 0:
# Backspaces don't delete already printed text with getch()
# "\b" is returned by getch() when Backspace key is pressed
response = response[:-1]
sys.stdout.write("\b \b")
elif c not in ["\r", "\b"]:
# Likewise "\r" is returned by the Enter key
response += c
sys.stdout.write(c)
elif c == "\r":
break
sys.stdout.flush()
return response
def print_(*args, sep=" ", end="\n"):
"""Print stuff on the same line."""
for arg in args:
if arg == inp:
input_()
else:
sys.stdout.write(arg)
sys.stdout.write(sep)
sys.stdout.flush()
sys.stdout.write(end)
sys.stdout.flush()
inp = None # Sentinel to check for whether arg is a string or a request for input
print_("I have", inp, "apples and", inp, "pears.")
@Jhran请从帖子中删除
,因为它们是不需要的。谢谢,我不知道格式设置在输入时也会这样。但是,文本只有在我输入两个数字后才会显示-但是,它应该显示到数字,例如“我有”-然后输入数字,从而继续文本。对不起,我没有提到:我有Python2.7。@Yinyue看到更新的答案。这个答案相当缺乏功能(没有向前删除、导航,除非通过退格、键盘中断、粘贴和行历史记录)。提供了更好的答案,(此处)[后者是此处答案的过度设计版本。
import sys
try:
from msvcrt import getwch as getch
except ImportError:
def getch():
"""Stolen from http://code.activestate.com/recipes/134892/"""
import tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def input_():
"""Print and return input without echoing newline."""
response = ""
while True:
c = getch()
if c == "\b" and len(response) > 0:
# Backspaces don't delete already printed text with getch()
# "\b" is returned by getch() when Backspace key is pressed
response = response[:-1]
sys.stdout.write("\b \b")
elif c not in ["\r", "\b"]:
# Likewise "\r" is returned by the Enter key
response += c
sys.stdout.write(c)
elif c == "\r":
break
sys.stdout.flush()
return response
def print_(*args, sep=" ", end="\n"):
"""Print stuff on the same line."""
for arg in args:
if arg == inp:
input_()
else:
sys.stdout.write(arg)
sys.stdout.write(sep)
sys.stdout.flush()
sys.stdout.write(end)
sys.stdout.flush()
inp = None # Sentinel to check for whether arg is a string or a request for input
print_("I have", inp, "apples and", inp, "pears.")