0

我对 python 有点陌生,我试图用格式化的字符串填充 raw_input,但我的输出永远不会改变“\n”字符并且总是返回 null。有没有办法通过空变量抽取完全格式化的字符串/文本?

text = ""
stop = "|"


while True:
   text_1 = raw_input()
   text += "%s" % (text1)
   if text_1 != stop:
     pass
   else:
     break


print text

output:
hello world
how are you
|
hello worldhow are you|

需要:你好,你好吗?

4

1 回答 1

0

我对“完全格式化的文本”的意思感到困惑。我也对“返回 null”的意思感到困惑。

raw_input返回读取的字符串,它可能是空字符串,但绝不应该是None. 顺便说一句,如果你熟悉NULL类 C 语言,None那么它在 Python 中是等价的。

这段代码与您的代码很接近,并且具有我认为您想要的行为。

text = ""
stop = "|"


while True:
  text_1 = raw_input()
  if text_1 != stop:
    text += text_1 + " "
  else:
    text += text_1
    break

print text
于 2016-12-18T11:02:27.687 回答