4 回答

TA貢獻1891條經驗 獲得超3個贊
對于輸入:
在循環中,問用戶一個單詞,如果沒有什么就停止
如果它是一個單詞,請將其保存為第一個字母(不是
sentence
acrostic
word[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

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)))

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)))

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
添加回答
舉報