【问题标题】:Python def vowelCount() counting the number of vowels [duplicate]Python def vowelCount()计算元音的数量[重复]
【发布时间】:2021-08-05 17:19:39
【问题描述】:

我已经定义了一个元音 count() 函数,该函数用于计算从元音 count() 函数返回计数的用户输入中元音的数量。我的 def 结构不起作用。

加法:如何不等于用户输入的任意数?

# Defining Function
def vowelCount(data):  # Counting the number of vowels in a word
    count = 0
    list1 = ["a", "e", "i", "o", "u"]
    for input in data:
        if input in list1:
            count += 1
    return count
while redo == True:
  try:
    userinput = str(input("Enter String Data: "))
    if (userinput >= 0) or (userinput <= 0):  # userinput should not be equal to a number
        print("2Please enter an alphabetical value.")
        break
    except:
        print("!Please enter an alphabetical value.")
        exit()
    print("Number of Vowels: ", vowelCount(userinput))

【问题讨论】:

  • 您的代码中有一些错误。首先,return 的缩进错误。
  • 您的return 语句在循环内,因此您在检查第一个字符后返回。它应该缩进一级。

标签: python


【解决方案1】:

我认为这是您缩进的问题。 return 应该与for 缩进相同。 至于查号,可以直接使用userinput.isalpha()。这会检查字符串中的每个元素是否都在字母表中。

【讨论】:

  • 感谢这个 Oplxy,非常有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 2021-12-22
  • 2021-12-12
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
相关资源
最近更新 更多