我想创建一个程序,它能理解你写的名言,并显示首字母高于"g“的单词,但我经常遇到一些复杂的问题。下面是我尝试过的两个代码:
这是我的第一个代码;
quote=input("Please input your quotes sentence.")
word=""
for letter in quote:
if word=="":
word=word+letter
elif word.lower().isalpha():
word=word+letter
elif word.lower().isalpha()==False:
if word[0].lower()>="h":
print(word.upper())
word=""
else:
word=""
和我的第二个代码;
for letter in quote:
if letter.lower().isalpha():
word=word+letter.lower()
elif letter.lower().isalpha()==False:
if letter.lower()>= "h":
print(word.upper())
word=""
elif letter.lower()<"h":
word=""
发布于 2018-07-31 18:40:17
我不确定我是否正确理解了你的问题,但是这样的事情怎么样:
# Input your quote here
quote = "A very import quote goes here"
out = [] # The output buffer
# Iterate over all words (seperated by <<space>>)
for word in quote.split(" "):
if word[0].lower() < "g": out += [word]
print (" ".join(out))
上面的代码将打印"A“,因为它是唯一一个以小于"g”的字母开头的单词。
发布于 2018-07-31 19:20:57
Juliusmh就在附近,但不是你想要的。因为您希望打印出整个单词(而不仅仅是G之前的第一个字母),所以您可以这样做:
# Input your quote here
quote = "A very import quote goes here"
out = "" # The output buffer
# Iterate over all words (seperated by <<space>>)
for word in quote.split(" "):
if word[0].lower() < "g": out += quote + " "
print (out)
发布于 2018-07-31 20:45:29
好了,在chromaerrors回答之后,我把第7列中的变量"quote“改成了word,我的问题就解决了。代码是这样的:
quote = input("A very import quote goes here")
out = [] # The output buffer
# Iterate over all words (seperated by <<space>>)
for word in quote.split(" "):
if word[0].lower() < "g": out += word + " "
print ("".join(out))
并且输出列出了起始字母小于g的所有单词。
谢谢你的帮助。:)
https://stackoverflow.com/questions/51611160
复制相似问题