0

私が抱えているこの問題について支援が必要です。プログラムで最初の Word から最初の文字を 1 行ごとに取得し、それらを 1 つの文字列に出力しようとしています。

たとえば、テキスト ブロックに次の単語を入力するとします。

People like to eat pie for three reasons, it tastes delicious. The taste is unbelievable, next pie makes a
great dessert after dinner, finally pie is disgusting.

結果は "Pg" になるはずです。これは小さな例ですが、お分かりいただけると思います。

コードから始めましたが、どこに行けばいいのかわかりません。

#Prompt the user to enter a block of text.
done = False
print("Enter as much text as you like. Type EOF on a separate line to finish.")
textInput = ""
while(done == False):
    nextInput= input()
    if nextInput== "EOF":
        break
    else:
        textInput += nextInput

#Prompt the user to select an option from the Text Analyzer Menu.
print("Welcome to the Text Analyzer Menu! Select an option by typing a number"
    "\n1. shortest word"
    "\n2. longest word"
    "\n3. most common word"
    "\n4. left-column secret message!"
    "\n5. fifth-words secret message!"
    "\n6. word count"
    "\n7. quit")

#Set option to 0.
option = 0

#Use the 'while' to keep looping until the user types in Option 7.
while option !=7:
    option = int(input())

#I have trouble here on this section of the code.
#If the user selects Option 4, extract the first letter of the first word
    #on each line and merge into s single string.
    elif option == 4:
        firstLetter = {}
        for i in textInput.split():
            if i < 1:
                print(firstLetter)
4

3 に答える 3

0

入力をリストとして保存し、各リストから最初の文字を取得できます。

textInput = []
while(done == False):
    nextInput= input()
    if nextInput== "EOF":
       break
    else:
        textInput.append(nextInput)



...


print ''.join(l[0] for l in textInput)
于 2013-07-15T02:07:33.590 に答える
0

単一の文字列ではなく、行のリストを作成することから始めます。

print("Enter as much text as you like. Type EOF on a separate line to finish.")

lines = []

while True:
    line = input()

    if line == "EOF":
        break
    else:
        lines.append(line)

次に、ループで最初の文字を取得できます。

letters = []

for line in lines:
    first_letter = line[0]
    letters.append(first_letter)

print(''.join(letters))

またはより簡潔に:

print(''.join([line[0] for line in lines]))
于 2013-07-15T02:07:53.063 に答える