循环

i7uq4tfw  于 2021-07-14  发布在  Java
关注(0)|答案(3)|浏览(202)

我希望我能得到一些帮助。这个程序的重点是,它应该采取一个由用户给出的句子元音计数。我不能让它增加计数,一切都保持在零。我希望有人能帮我指出我把代码弄乱的地方。它必须保持非常相似的格式,for循环直接在字符串上迭代。我试过用很多不同的方法来操纵它。如果有人能够帮助我张贴一个我的代码。谢谢!

VOWELS = 'AEIOU'
def count3(string, letter):
  count = 0
  # for loop only loops over index, don't initialize or incriminate index
  for char in string:
    #letters = string[char]
    letter_low = str.lower(letter)
    if char == letter_low:
      count = count + 1
  return (letter + " : %d" % count)
  # come back to this, not increasing count of each vowel

def main():
  print("Enter a sentence and this sentence will display its vowel count.")
  sent = input("Enter the sentence to be analyzed:  ")
  while sent:
    print("Your sentence was: " + sent)
    sent_low = str.lower(sent)
    print("\nAnalysis")

  for letter in VOWELS:
    print(count3(sent_low, letter))
3pvhb19x

3pvhb19x1#

我想你的代码需要清理一下。

def count3(string):
   vowels = ['a','e','i','o','u']
   count = 0
   for char in string:
      if char in vowels:
         count += 1
   return count
def main():

   sent = input("Enter a sentence and this sentence will display its vowel count:  ")
   print("Your sentence was: " + sent)
   sent_low = sent.lower()
   vowels = count3(sent_low)
   print(f"Your string has {vowels} number of vowels")

main()
aor9mmx1

aor9mmx12#

如果你用了 .count() 方法?

def count3(string):
    count = 0
    vowels = ['a', 'e', 'i', 'o', 'u']
    string_as_list = list(string.lower())
    for vowel in vowels:
        count += string_as_list.count(vowel)
    return count

def main():
    while True:
        print("Enter a sentence and this sentence will display its vowel count.")
        sent = input("Enter the sentence to be analyzed:  ")
        print(count3(sent))
    return None

main()
llmtgqce

llmtgqce3#

在我删除 while 语句,这导致了一个无限循环。


# Exactly the same as your code, with the while statement removed

VOWELS = 'AEIOU'

def count3(string, letter):
    count = 0
    # for loop only loops over index, don't initialize or incriminate index
    for char in string:
        # letters = string[char]
        letter_low = str.lower(letter)
        if char == letter_low:
            count = count + 1
    return (letter + " : %d" % count)
    # come back to this, not increasing count of each vowel

def main():
    print("Enter a sentence and this sentence will display its vowel count.")
    sent = input("Enter the sentence to be analyzed:  ")

    print("Your sentence was: " + sent)
    sent_low = str.lower(sent)
    print("\nAnalysis")

    for letter in VOWELS:
        print(count3(sent_low, letter))

### Example output if I run main() and enter "this is a test string"

# Analysis

# A : 1

# E : 1

# I : 3

# O : 0

# U : 0

然而。。。有更好的方法来完成你想要完成的事情。例如,下面是如何利用 .count() 方法。这将产生与代码相同的结果,同时消除for循环。

def count_occurrences(string, letter):
    return string.lower().count(letter.lower())

def main():
    print("Enter a sentence and this sentence will display its vowel count.")
    sent = input("Enter the sentence to be analyzed:  ")

    print("Your sentence was: " + sent)

    for letter in VOWELS:
        count = count_occurrences(sent, letter)
        print(f"{letter}: {count}")

如果你坚持不使用 .count() 方法,仍然可以简化代码。例如:

def count_occurrences(string, letter):
    return len([x for x in string if x.lower() == letter.lower()])

相关问题