这是我到目前为止的代码。该程序应该询问用户一个单词并确定其中元音/辅音的数量。用户完成后,程序应该给他们元音/辅音的平均数量。一些开始的代码是不同的(在V
and之间不同C
),因为我试图找出哪个可以工作
#Okay, this is the updated version of the code where I combined @AshwiniChaudhary 's code and mine
现在我得到一个 IndexError: tuple index out of range
我试着在这里和其他几个网站上查找它,但并没有真正找到答案,我不想在这里问同样的问题两次
print("Hello!")
import sys
import re
def VOCO():
VOCO = input("Search for VOWELS or CONSONANTS: ")
if VOCO == "VOWELS":
VOW = input("Please input word for VOWEL counting: ")
re.findall(r'[aeiouAEIOU]', VOW)
V = int(len(re.findall(r'[aeiouAEIOU]', VOW)))
print(V)
elif VOCO == "CONSONANTS":
CON = input("Please input word for CONSONANT counting: ")
re.findall(r'[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]', CON)
C = int(len(re.findall(r'[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]', CON)))
print(C)
else:
print("Please use either VOWELS or CONSONANTS in selection.")
from string import ascii_lowercase as al
def find_vo_cons2(strs,val):
vowels="aeiou"
consonants=set(al)-set(vowels) #set containing only consonants
le=float(len(strs))
vowel_count=0
consonant_count=0
for x in strs:
if x.lower() in vowels:
vowel_count+=1
elif x.lower() in consonants:
consonant_count+=1
if val in ("V","v"):
return vowel_count/le
elif val in ("C","c"):
return consonant_count/le
def CNT(): True or False
CNT = input("Would you like to continue (YES/NO)? ")
if CNT() : "YES"
condition(True)
VOCO()
else:
strs = (VOW or CON)
print("The average of {0} count is {1}".format(find_vo_cons2(strs,VOCO)))
sys.exit
VOCO()
while True:
VOCO()