这是我的任务。
创建一个程序输入一个短语(如一个着名的引文)并打印所有以h-z开头的单词。
这是我的代码到目前为止,但它仍会打印以" g"开头的单词。并且不打印最后一个单词。
famous_quote = input("Enter a one sentence quote: ").lower()
word = ""
for ltr in famous_quote:
if ltr.isalpha() == True:
word = word + ltr
else:
if word > "g":
print(word)
word = ""
else:
word = ""
我只允许使用ASCII比较,我试图比较ASCII值,但我不知道如何在这种情况下进行比较。
以下是我得到的进一步说明,我们还没有被教过使用split()函数,所以我们无法使用它。
示例输入: 输入一个句子引用,非alpha单独的单词:无论你走到哪里,全心全意地去做
示例输出:
WHERESOEVER
YOU
WITH
YOUR
HEART
答案 0 :(得分:3)
>
执行lexocographic比较...... good
恰好大于g
。所以它被打印出来了。此外,缓冲区中的最后一个单词永远不会打印,因为循环完成迭代而不会达到条件(非字母),因此永远不会检查和打印缓冲区。
您可以将句子分成单词,然后测试每个单词的起始字符:
words = "good things come to those who wait".lower().split()
for word in words:
if word[0] >= 'h':
print(word, end=' ')
# "things to those who wait"
答案 1 :(得分:2)
我在user_input中添加了空格,并且还使用了单词>'h'。下面是它的外观:
user_input = input('Enter a phrase: ').lower()
user_input += ' '
word = ''
for char in user_input:
if char.isalpha():
word += char
else:
if word > 'h':
print(word.upper())
word = ''
else:
word = ''
答案 2 :(得分:1)
这段代码对我有用...... 任务是:创建一个程序输入一个短语(如一个着名的引文)并打印所有以h-z开头的单词
我犯了使用word>的错误之前的“g”,需要用“>”替换。 “H”。 此外,您需要添加最后一个打印命令,以便在短语没有以标点符号结尾的情况下打印最后一个单词(如给定示例中所示)
phrase = input ("Please enter a phrase: ").lower()
word = ""
for letter in phrase:
if letter.isalpha():
word += letter
else:
if(word > "h" ):
print(word)
word = ""
else:
word = ""
print(word)
答案 3 :(得分:1)
这就是我解决这个问题的方法。自从我成为一名初学者以来,这让我很难过。但似乎工作正常。
quote = "quote goes here"
word = ""
for letter in quote:
if letter.isalpha():
word += letter
else:
if word > "":
print(word.upper())
word = ""
else:
word = ""
print(word.upper())
答案 4 :(得分:0)
假设着名的引语只包含空格作为单词分隔符,这应该可以完成这项工作:
words = input("Enter a one sentence quote: ").lower().split()
for word in words:
if word[0] > 'g':
print("{w} ".format(w = word))
split()将字符串转换为列表(数组)。默认情况下,它将空格字符作为参数(因此我没有给出参数)并返回单词列表。
print()可以通过多种方式使用,因为python具有此功能的历史记录。
你可以.join()列表(得到一个字符串作为结果)并打印出来:
print(" ".join(words))
你也可以用连词打印(被认为是丑陋的):
print(word+" ")
或者您可以使用格式化打印,我会大量使用它来实现可读性:
print("{w} ".format(w = word))
解释" {w}"并将 word 替换为" {w}"出现。
打印格式化相当耗费CPU(但它仍然非常快)。通常任何打印操作都会减慢您的应用程序,如果您将来制作CPU密集型应用程序,您希望最大限度地减少输出(此处我不这样做,因为CPU不是主要问题)。
答案 5 :(得分:0)
只有一个关于练习的评论,作为编程练习,这种方法很好,但你在实践中绝不会这样做。
您突出显示的两个问题是您正在比较整个单词而不仅仅是第一个单词 只需更改:
if word > "g":
要:
if word and word[0] > "g":
如果引号没有用标点符号结束,你将错过最后一个单词,只需在循环后添加:
if word:
print(word)
您可能会注意到输出全部为大写,因此.lower()
整个报价可能是一个问题,或者您可以.lower()
进行比较,例如:
famous_quote = input("Enter a one sentence quote: ")
...
if word and word[0].lower() > "g":
注意:您可以简化else:
条件:
else:
if word and word[0] > "g":
print(word)
word = ""
答案 6 :(得分:0)
您声明不允许使用split()
方法。我不确定你可以使用什么,所以这是一个解决方案(不是最佳解决方案)。
famous_quote = input("Enter a one sentence quote:") + ' '
current_word = None
for c in famous_quote:
if ('a' <= c <= 'z') or ('A' <= c <= 'Z'):
if current_word is None:
current_word = c # start a new word
else:
current_word += c # append a new letter to current word
else:
if current_word is not None:
f = current_word[0] # first letter
if ('h' <= f <= 'z') or ('H' <= f <= 'Z'):
print(current_word)
current_word = None
以下是该程序的示例运行。它保留小写和大写。它还会拆分任何非ASCII字符上的单词。
Enter a one sentence quote: Whereever you go, there you are!!!
Whereever
you
there
you
注意:由于在遇到非ASCII字符时进行打印,因此在famous_quote
的末尾附加非ASCII字符。
答案 7 :(得分:0)
1。通过构建占位符变量来拆分单词:word
循环输入字符串中的每个字符 并检查字符是否是一个字母。然后在变量&#34; word&#34;中添加字母。循环直到遇到非alpha字符。
2。如果字符是alpha或(字母)
将字符添加到单词中。 检测到非alpha(空格,标点符号,数字......)定义单词的结尾并转到&#34; else&#34;一部分。
input_quote = input("Enter a 1 sentence quote, non - alpha seperate words: ")
word = ""
for character in input_quote:
if character.isalpha():
word += character
3。其他
检查单词是否大于&#34; g&#34;按字母顺序排列。打印单词并设置&#34; word = empty&#34;字符串。
else:
if word and word[0].lower() >= "h":
print("\n", word.upper())
word = ""
4。或者
设置word =空字符串并构建下一个单词。
else:
word = ""
if word.lower() >= "h":
print("\n", word.upper())
最后一个&#34; if&#34;如果它不以像空格或标点符号这样的非字母字符结尾,则显式编码以打印最后一个单词。
答案 8 :(得分:0)
我做了同样的问题。大多数人所拥有的问题(似乎没有人指出)是你遇到双重标点符号或标点后跟空格。
这是我使用的代码。
phrase = input("Please enter a famous quote: ")
word = ""
for letter in phrase:
if letter.isalpha() is True:
word += letter
elif len(word) < 1: <--- [This is what accounts for double punctuations]
word = ""
elif word[0].lower() >= "g":
print(word)
word = ""
else:
word = ""
print(word) <--- [accounts for last word if not punctuated]
答案 9 :(得分:0)
变量“单词”已包含短语的最后一个单词,但由于它不符合进入循环的条件,因此不会打印。所以你可以查看下面的解决方案。
phrase = input("Enter a phrase after this: ")
word = ""
for char in phrase:
if char.isalpha():
word += char
else:
if word != "":
if word[0].lower() >= "h":
print(word.upper())
word = ""
else:
word = ""
if word[0].lower() >= "h":
print(word.upper())
答案 10 :(得分:0)
此代码对我有用:
phrase=input("Enter a one sentence quote,non-alpha separate words: ")
word=""
for character in phrase:
if character.isalpha():
word+=character
else:
if word.lower()>="h".lower():
print(word.upper())
word="" -----this code defines the end of a word
else:
word=""
print(word.upper()) ------this will print the last word
答案 11 :(得分:0)
我将使用regular expressions
和list compreshension
,如下面的函数所示。
def words_fromH2Z():
text = input('Enter a quote you love : ')
return [word for word in re.findall('\w+', text) if not word[0] in list('aAbBcCdDeEfFgG')]
当我通过输入“我总是访问堆栈溢出以寻求帮助”来测试功能时,我得到:
words_fromH2Z()
Enter a quote you love : I always Visit stack Overflow for Help
['I', 'Visit', 'stack', 'Overflow', 'Help']
答案 12 :(得分:0)
这对我来说很好。我必须添加最后两行代码,因为如果没有它们,它就不会打印最后一个单词,即使它以h和z之间的字母开头。
word = ""
quote = input("Enter your quote")
for char in quote:
if char.isalpha():
word += char
elif word[0:1].lower() > "g":
print(word.upper())
word = ""
else:
word = ""
if word[0:1].lower() > "g":
print(word.upper())
答案 13 :(得分:-1)
famous_quote = input("Enter a one sentence quote:")
current_word = None
for c in famous_quote:
if c.isalpha():
if (c >= 'a') or (c >= 'A'):
if current_word is None:
current_word = c
else:
current_word += c
else:
if current_word is not None:
f = current_word[0]
if ('h' <= f <= 'z') or ('H' <= f <= 'Z'):
print (current_word.upper())
current_word = None
if famous_quote[-1].isalpha():
print (current_word.upper())