这个 c++ 程序的 Python 等效性



我在解决python中的问题时遇到了麻烦,因为我在python中找不到任何等价物(getline(((。因此,即使我解决了我的问题,由于正确的输入方式,它们也不被接受。我怎样才能用python重新编码?尤其是同时块!

#include <iostream>
#include <string>
using namespace std;
string sentences[105];
int main()
{
int pos = 0;
int longest = 0;
while (getline(cin, sentences[pos]))
{
if (sentences[pos].size() > longest)
longest = sentences[pos].size();
++pos;
}
for (int j = 0; j < longest; ++j)
{
for (int i = pos - 1; i >= 0; --i)
{
if (sentences[i].size() > j)
cout << sentences[i][j];
else
cout << ' ';
}
cout << 'n';
}
}

这是我的蟒蛇代码。

while True:
try:
lst = []
n = 0
while True:
line1 = [' ']*100
line = input()
if n < len(line):
n = len(line)
for i in range(len(line)):
line1[i] = line[i]
if line:
lst.append(line1)
else:
break
for i in range(0,n):
x = '' 
for j in range(len(lst)-1,-1,-1):
x += lst[j][i]
print(x)
except EOFError:
break

Python 2.7中,你有:

line = raw_input("Please input a new line: ")

Python 3.5+中,你有:

line = input("Please input a new line: ")

raw_inputinput都返回一个字符串对象。 您需要解析/扫描line才能检索数据。

MESSAGE = "Please input a new line: "
# I comprehend that `sentences` is a list of lines
sentences = []
longest = 0
line = input(MESSAGE)
while line:
# Loop continues as long as `line` is available
# Keep a track of all the lines
sentences.append(line)
# Get the length of this line
length = len(line)
if length > longest:
longest = length
for i in range(longest):
for j in range(len(sentences) - 1, -1, -1):
if len(sentences[j]) > i:
print(sentences[j][i])
line = input(MESSAGE)

最新更新