python只识别我输入的第一行(python only recognizes first line of my input)
使用此代码:
A = raw_input("Please input text here")
print A
print len(A)
对于input ,当我从记事本复制和粘贴时,它只识别以下第一行:
Cars are fast
Motorcycles are faster
Planes are even faster
因此, print A将print '汽车快速'并print len(A)与print 13 ,这是'汽车快速'的长度。
如何让Python识别input的剩余行?
谢谢。
With this code:
A = raw_input("Please input text here")
print A
print len(A)
For the input, when I copy and paste from notepad, it only recognizes the first line of the following:
Cars are fast
Motorcycles are faster
Planes are even faster
Therefore, print A will print 'Cars are fast' and print len(A) with print 13, which is the length of 'Cars are fast'.
How can I get Python to recognize the remaining lines of my input?
Thank you.
原文:https://stackoverflow.com/questions/22440205
更新时间:2019-10-27 18:10
最满意答案
有关从命令行获取输入的一般概述,请参阅此问题的答案。
如果你想使用raw_input,那么一个循环会读取你所有的行,但是你需要一种方法来突破,就像这样。
while True:
A = raw_input("Please input text here (Q to quit)")
if len(A) == 1 and A[0].lower() == "q":
break
print A
print len(A)
要整理多行,请执行以下操作
data = []
while True:
A = raw_input("Please input text here (Q to quit)")
if len(A) == 1 and A[0].lower() == "q":
break
data.append(A)
print data
for A in data:
print len(A)
请记住在粘贴后输入换行符。 此外,raw_input提示消息可能无法正确显示。
你可能会疯狂并管理提示。 期望零长度输入意味着用户正在尝试退出。
data = []
prompt = "Please input text (Q to quit):\n"
while True:
if data:
A = raw_input()
else:
A = raw_input(prompt)
while len(A) == 0:
A = raw_input(prompt)
if len(A) == 1 and A[0].lower() == "q":
break
data.append(A)
for A in data:
print "%s - %i" % (A, len(A))
raw_input will read one line of input only.
See answers to this question for a general overview of getting input from the command line.
If you want to use raw_input then a loop will read all your lines but you will need a way to break out, something like this.
while True:
A = raw_input("Please input text here (Q to quit)")
if len(A) == 1 and A[0].lower() == "q":
break
print A
print len(A)
To collate multiple lines do something like this
data = []
while True:
A = raw_input("Please input text here (Q to quit)")
if len(A) == 1 and A[0].lower() == "q":
break
data.append(A)
print data
for A in data:
print len(A)
Remember to enter a newline after pasting. Also, the raw_input prompt messages may not display correctly.
You could go crazy and manage the prompt. Expecting a zero length input means the user is trying to quit.
data = []
prompt = "Please input text (Q to quit):\n"
while True:
if data:
A = raw_input()
else:
A = raw_input(prompt)
while len(A) == 0:
A = raw_input(prompt)
if len(A) == 1 and A[0].lower() == "q":
break
data.append(A)
for A in data:
print "%s - %i" % (A, len(A))
2017-05-23
相关问答
您可以使用单例生成器/列表技巧避免两次调用函数,但仍可重用其结果: print(next(x if x == 'a' else 'Not a' for x in [input()]))
当你处于这种状态时, x if y else z构造变得更神秘,你也可以缩短三元x if y else z :-) print(next(('Not a', x)[x == 'a'] for x in [input()]))
但更少的线路本身并不是目的。 你拥有的五条线是完全有效和可读的 。 You can u
...
这里的主要问题不是性能,而是可读性和漂亮的代码。 我建议你创建一个单独的函数来检查单元格的位置,并在main函数中调用该函数。 这样,您可以在一行中检查单元格的位置。 它简单,快速,易于阅读。 因为你可以在编辑器中保存函数并关闭脚本,就像调用内置单行程序一样。 该函数还可以执行其他输入检查。 功能示例: function idx = cell_index(C)
idx = 0;
if isempty(C)
warning('No input was given.')
else
fo
...
您实际上不需要实现didSelectRowAtIndexPath:如果您将故事板中的segue从单元格连接到下一个控制器。 你需要实现的是prepareForSegue: 您的代码应如下所示: - (void)prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender
{
NSInteger row = [self.tableView indexPathForSelectedRow].row;
SecondViewC
...
您使用的终端程序是什么? 它可能有一个设置,您可以使用该设置控制退格键的解释方式。 在gnome-terminal中,如果单击Edit>Profile Preferences>Compatibility ,则可以告诉gnome-terminal在按下退格键时发送ASCII DEL而不是Ctrl-h。 否则,我认为你可以用类似的东西来修复你的键盘映射 xmodmap -e "keycode 22 = BackSpace"
然而,我的记忆生疏了。 有关详细信息,请参阅HOWTO和/或本指南 。 Wh
...
最有可能的是,您已将模块安装到与jupyter命令使用的环境不同的环境中(例如,如果“myenv3”中没有jupyter ,则将使用PATH上的其他任何内容)。 有关故障排除提示,请参阅例如Keras导入错误Nadam 。 Most probably, you've installed the module to a different environment than the one your jupyter command is using (e.g. if there' no jupyter
...
这是基于我对你的其他问题的回答 def line_and_line_before(file):
prev_line = None
for line in file:
yield (prev_line, line)
prev_line = line
def has_ones(line):
splitted_line = line.split(" ")
return len(splitted_line) > 4 and splitted
...
您正在将字符串传递给Polygon ,但它需要一个坐标列表(数字)。 尝试这个: import ast
line = ast.literal_eval(f.readline())
You're passing a string to Polygon, but it's expecting a list of coordinates (numbers). Try this: import ast
line = ast.literal_eval(f.readline())
对于第一个问题, str.count()没有发现重叠匹配,正如你注意到的那样。 相反,使用str.find()并在每次找到匹配之后提前开始索引,直到结果为-1 ,例如: >>> 'assesses'.find('sses', 0) # first look at the start of the string
1
>>> 'assesses'.find('sses', 2) # now look at previous index + 1
4
>>> 'assesses'.find('
...
您的文件包含: ” 而你的词法分析器寻找: “ 这些是截然不同的 Your file contains: ” whereas your lexer looks for: " These are distinct.
raw_input只读取一行输入。 有关从命令行获取输入的一般概述,请参阅此问题的答案。 如果你想使用raw_input,那么一个循环会读取你所有的行,但是你需要一种方法来突破,就像这样。 while True:
A = raw_input("Please input text here (Q to quit)")
if len(A) == 1 and A[0].lower() == "q":
break
print A
print len(A)
...