亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

打印出列表中每個項目的首字母

打印出列表中每個項目的首字母

米脂 2022-09-06 17:26:40
我正在嘗試創建一個程序,要求輸入單詞,直到輸入“”。然后,程序將打印出一個句子中連接的所有單詞。然后取每個單詞的第一個字母來做一個離合詞。我正在使用python。示例如下所示。提前感謝您。這很快就會到期。:)我編碼的內容:sentence = []acrostic = []word = -1while word:     sentence.append(word)      acrostic.append(sentence[0].upper())print(sentence)print("-- {}".format(acrostic))我希望代碼做什么:Word: AWord: crossWord: tickWord: isWord: veryWord: evilWord: A cross tick is very evil-- ACTIVE
查看完整描述

4 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

對于輸入:

  • 在循環中,問用戶一個單詞,如果沒有什么就停止

  • 如果它是一個單詞,請將其保存為第一個字母(不是sentenceacrosticword[0]sentence[0])

對于輸出:

  • 對于句子,用空格連接單詞:" ".join(sentence)

  • 對于離合詞,將字母與任何東西連接在一起:"".join(acrostic)

sentence = []

acrostic = []

while True:

    word = input('Please enter a word, or enter to stop : ')

    if not word:

        break

    sentence.append(word)

    acrostic.append(word[0].upper())


print(" ".join(sentence))

print("-- {}".format("".join(acrostic)))


Please enter a word, or " to stop : A

Please enter a word, or " to stop : cross

Please enter a word, or " to stop : tick

Please enter a word, or " to stop : is

Please enter a word, or " to stop : very

Please enter a word, or " to stop : evil

Please enter a word, or " to stop : 

A cross tick is very evil

-- ACTIVE


查看完整回答
反對 回復 2022-09-06
?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

python 3.8 或更高版本

sentence = []

acrostic = []

while user_input := input('word: '):

    sentence.append(user_input)

    acrostic.append(user_input[0].upper())


print(' '.join(sentence))

print(f"-- {''.join(acrostic)}")

輸出:


word: A

word: cross

word: tick

word: is

word: very

word: evil

word: 

A cross tick is very evil

-- ACTIVE

python 3.6 和 3.7

sentence = []

acrostic = []

while True:

    user_input = input('word: ')

    if not user_input:

        break

    sentence.append(user_input)

    acrostic.append(user_input[0].upper())


print(' '.join(sentence))

print(f"-- {''.join(acrostic)}")

python 3.5 或更早版本

sentence = []

acrostic = []

while True:

    user_input = input('word: ')

    if not user_input:

        break

    sentence.append(user_input)

    acrostic.append(user_input[0].upper())


print(' '.join(sentence))

print('-- {}'.format(''.join(acrostic)))


查看完整回答
反對 回復 2022-09-06
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

也許你正在尋找這樣的東西:


sentence = []

acrostic = []

word = -1


while word != "":

    word = input("Word: ")


    if word:

        sentence.append(word)

        acrostic.append(word[0].upper())


print(" ".join(sentence))

print("-- {}".format("".join(acrostic)))


查看完整回答
反對 回復 2022-09-06
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

雖然每個人都有基本循環的覆蓋,但這是一個使用迭代器(可調用,sentinel)模式的不錯示例:


def initial():

    return input('Word: ')[:1]


print('-- ' + ''.join(iter(initial, '')))

將產生:


Word: A

Word: cross

Word: tick

Word: is

Word: very

Word: evil

Word: 

-- Active


查看完整回答
反對 回復 2022-09-06
  • 4 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號