[英]Why input() function in python gets only first written object?
For example:例如:
a = input()
print(a)
Then, I am inputting those separated words written: separated In output, I am only getting "4"然后,我输入那些分开的文字:分开在output,我只得到“4”
But when I am inputting them like this: not separated但是当我像这样输入它们时:不分开
I am getting each word.我得到每一个字。
How can I get each word by inputting separately?如何通过单独输入获得每个单词?
You need a separate input()
for each line, so for 2 sentences you will need:每行都需要一个单独的
input()
,因此对于 2 个句子,您将需要:
a = input()
b = input()
print(a)
print(b)
In case you are expecting multiple inputs, but you dont know how many you can place the input()
inside a loop until an exit condition is met如果您期望多个输入,但您不知道可以将多少个
input()
放入循环中,直到满足退出条件
line = ""
while line != "exit":
line = input()
# Do whatever with line
input()
reads just one line.只读一行。 To read all the lines, you need to use for example:
要阅读所有行,您需要使用例如:
import fileinput
for line in fileinput.input():
<do sth>
if your variable is meant to be string, you can easily concatenate your strings with +.如果你的变量是字符串,你可以很容易地用 + 连接你的字符串。 If the variable should later be printed and give the same output as the given input, you can use '/n':
如果稍后应该打印变量并给出与给定输入相同的 output,则可以使用“/n”:
my_var = ""
for line in fileinput.input():
my_var += '/n' + line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.