[英]print out first letter of each item in a list
I am trying to create a program that asks for a word input until '' is entered.我正在尝试创建一个程序,在输入 '' 之前要求输入一个单词。 Then the program will print out all of the words joined in a sentence.然后程序将打印出一个句子中连接的所有单词。 Then take the first letter of each word to make an acrostic.然后取每个单词的第一个字母来制作一个离合词。 I am using python.我正在使用蟒蛇。 Example shown below.示例如下所示。 Thank you in advance.先感谢您。 This is due really soon.这很快就要到期了。 :) :)
sentence = []
acrostic = []
word = -1
while word:
sentence.append(word)
acrostic.append(sentence[0].upper())
print(sentence)
print("-- {}".format(acrostic))
Word: A
Word: cross
Word: tick
Word: is
Word: very
Word: evil
Word:
A cross tick is very evil
-- ACTIVE
For input:对于输入:
sentence
and it's first letter in acrostic
( word[0]
not sentence[0]
)如果是单词,则将其保存在sentence
,并且是acrostic
的第一个字母( word[0]
不是sentence[0]
)For output:对于输出:
" ".join(sentence)
对于句子用空格连接单词: " ".join(sentence)
"".join(acrostic)
对于离合词,将字母加入空字符: "".join(acrostic)
.join "".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)))
Gives给
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
sentence = []
acrostic = []
while user_input := input('word: '):
sentence.append(user_input)
acrostic.append(user_input[0].upper())
print(' '.join(sentence))
print(f"-- {''.join(acrostic)}")
output:输出:
word: A
word: cross
word: tick
word: is
word: very
word: evil
word:
A cross tick is very evil
-- ACTIVE
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)}")
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)))
Maybe you're looking for something like this:也许你正在寻找这样的东西:
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)))
Goal: To extract first letter of each word untill '' is entered.目标:提取每个单词的第一个字母,直到输入 ''。
acrostic = ""
sentence = ""
while(True):
word = input("enter a word= ")
if(word!=''):
acrostic+=word[0].upper()
sentence+=word+" "
else:
break
print(sentence)
print(acrostic)
With your sample input;使用您的样本输入;
enter a word= A
enter a word= cross
enter a word= tick
enter a word= is
enter a word= very
enter a word= evil
enter a word=
output输出
A cross tick is very evil
ACTIVE
While everybody has you covered for basic loops, this is a nice example to use the iter(callable, sentinel)
pattern:虽然每个人都了解基本循环,但这是使用iter(callable, sentinel)
模式的一个很好的例子:
def initial():
return input('Word: ')[:1]
print('-- ' + ''.join(iter(initial, '')))
Will produce:将产生:
Word: A
Word: cross
Word: tick
Word: is
Word: very
Word: evil
Word:
-- Active
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.