【问题标题】:How to Count Vowels and Consonants in an Input Using Arrays?如何使用数组计算输入中的元音和辅音?
【发布时间】:2013-08-15 00:13:13
【问题描述】:

这是我的任务:

编写一个程序,从键盘读取文本直到'!'找到了。

使用由字母“A”到“Z”下标的整数数组, 计算每个字母出现的次数(不管它是否 是大写还是小写)。在一个单独的计数器中,也计算总数 “其他”字符的数量。

打印出找到的每个字母的计数。另外,打印非字母的计数 字符。

通过检查数组,打印出元音的个数, 和辅音的数量。

这是我的代码:

msg = input("What is your message? ")

print ()

num_alpha = 26
int_array = [0] * num_alpha
vowel = [0] * 10000
consanant = [0] * 10000

for alpha in range(num_alpha):
    int_array[alpha] = chr(alpha + 65)
    if int_array[alpha] == 'A' or int_array[alpha] == 'E' or int_array[alpha] == 'I' or int_array[alpha] == 'O' or int_array[alpha] == 'U':
        vowel[alpha] = int_array[alpha]
        print(vowel[alpha])
    else:
        consanant[alpha] = int_array[alpha]



print()

lett = 0
otherch = 0
num_vowels = 0
num_consanants = 0

count_character = [0] * 100000

length = len(msg)

for character in msg.upper():
    if character == "!":
        otherch = otherch + 1
        count_character[ord(character)] = count_character[ord(character)] + 1
        break
    elif character < "A" or character > "Z":
        otherch = otherch + 1
        count_character[ord(character)] = count_character[ord(character)] + 1
    else:
        lett = lett + 1
        count_character[ord(character)] = count_character[ord(character)] + 1
        if vowel[(alpha)] == (character):
            num_vowels = num_vowels + 1
            print(vowel[alpha])
        else:
            num_consanants = num_consanants + 1

print("Number of Letters =", lett)
print("Number of Other Characters = ", otherch)
print("Number of Vowels = ", num_vowels)
print("Number of Consanants = ", num_consanants)


for character in msg.upper():
        print("Character", character, "appeared" , count_character[ord(character)] , "time(s).")
        if character == "!":
            break

每次我输入一个字符串时,它都无法识别元音。如果我输入“abe!”它会打印:

Number of Letters = 3 
Number of Other Characters =  1 
Number of Vowels=  0 
Number of Consanants =  3 
Character A appeared 1 time(s). 
Character B appeared 1 time(s). 
Character E appeared 1 time(s).
Character ! appeared 1 time(s).

【问题讨论】:

  • 作业写得不好。您不能用字符串为“数组”下标。还有另一个提示:数字 10000 不应出现在您的代码中。
  • 那我应该使用什么号码? ascii 表中的字符数?
  • 这个作业是用 Python 还是 C 来完成的?它读起来就像编写它的人从未接触过 Python。
  • @user 你不应该需要任何这样的数字。

标签: python arrays list char


【解决方案1】:
if vowel[(alpha)] == (character):
  num_vowels = num_vowels + 1
  print(vowel[alpha])

在此代码中,您的 alpha 超出范围,这意味着 alpha 将是前一个 for 循环的最后一次迭代中的任何内容

另外我建议使用in检查元音的更好方法

vowels = ['a','e','i','o','u']
char = 'a'
if char in vowels:
  pass              # you have found a vowel

【讨论】:

  • @user2680935 我不认为它在做你认为它在做的事情。我建议您更改检查元音的方式
  • 您可以将其进一步缩短为 char in 'aeiou'
  • @antimony 在这种情况下我不支持硬编码
【解决方案2】:

这里你需要分配alpha。否则,它将在顶部采用 for 循环的最后一个值(因此它将是 25)。

else:
    lett = lett + 1
    count_character[ord(character)] = count_character[ord(character)] + 1
    alpha = ord(character) - ord('A') # <-- need this
    if vowel[(alpha)] == (character):
        num_vowels = num_vowels + 1
        print(vowel[alpha])
    else:
        num_consanants = num_consanants + 1

【讨论】:

  • 我建议您继续查看 Stephan 给出的答案,因为它将帮助您理解更简单、更简洁的方法。
猜你喜欢
  • 2021-08-07
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多