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
添加回答
举报