Напишите программу, которая принимает строку ввода пользователя и выводит каждое второе слово - PullRequest
0 голосов
/ 25 февраля 2019

Пожалуйста, введите предложение: быстрая коричневая лиса перепрыгивает через ленивую собаку.

Вывод: Браун прыгает за собакой

Я изучал строки в python, но, что бы я ни делал, я не могу написать программу, которая будет удалять2-я буква каждого предложения.

word=(input ("enter setence"))

del word[::2]

print(word[char], 
end="")

Print("\n")

Это была моя ближайшая попытка.По крайней мере, я смог написать предложение в командной строке, но не смог получить требуемый вывод.

Ответы [ 4 ]

0 голосов
/ 25 февраля 2019

Попробуйте что-нибудь подобное.

sentence = input("enter sentence: ") words = sentence.split(' ') print(words[::2])

0 голосов
/ 25 февраля 2019

Попробуйте что-то вроде:

" ".join(c for c in word.split(" ")[::2])

0 голосов
/ 25 февраля 2019

Попробуйте это:

sentenceInput=(input ("Please enter sentence: "))

# Function for deleting every 2nd word
def wordDelete(sentence):

    # Splitting sentence into pieces by thinking they're seperated by space.
    # Comma and other signs are kept.
    sentenceList = sentence.split(" ")

    # Checking if sentence contains more than 1 word seperated and only then remove the word
    if len(sentenceList) > 1:
        sentenceList.remove(sentenceList[1])

    # If not raise the exception
    else:
        print("Entered sentence does not contain two words.")
        raise Exception

    # Re-joining sentence
    droppedSentence = ' '.join(sentenceList)

    # Returning the result
    return droppedSentence

wordDelete(sentenceInput)
0 голосов
/ 25 февраля 2019
string = 'The quick brown fox jumps over the lazy dog.'
even_words = string.split(' ')[::2]

Вы разделяете исходную строку пробелами, а затем берете каждое другое слово из нее с помощью [:: 2] сращивания.

...