没有元音的单词

Words without Vowels

在这段代码中,基本上我试图计算这句话中没有元音但有些地方(或者可能是所有地方)我做错了的单词,这是代码

par="zyz how are you"
count=0

for i in range(len(par)):
    if par[i]==" ":
        if par[i]!="a" or par[i]!="e" or par[i]!="i" or par[i]!="o" or par[i]!="u":
            count+=1
        
print("total words without vowel -> ",count)

当您使用 len(par) 时,它 returns 字符串中有多少字母。相反,您必须使用 par = "zyz how are you".split(" ")

逐字拆分字符串

拆分后,您将得到 par 作为包含 ["zyz","how","are","you"]

的列表

现在你可以只检查单词中是否有元音,而不是遍历每个字母

par = "zyz how are you".split(" ")
count = 0

for i in range(len(par)):
    if "a" in par[i] or "e" in par[i] or "i" in par[i] or "o" in par[i] or "u" in par[i]:
        pass
    else:
        count += 1

print("total words without vowel ->",count)

这是我为您编写的代码,可能不是最好的,但可以运行并添加注释,因此很容易理解!

par = "zyz how are you" # Define string variable with your sentence

def countwordsnowovels(string): # Define function that does it with argument called string
    count = 0 # Set the count to 0
    wordlist = string.split(" ") # Split the string into a list with every word as an entry
    for word in wordlist: # for every entry in the list "wordlist"
        if "a" not in word: # Check if there is no letter a in the word
            if "e" not in word: # Check if there is no letter e in the word
                if "i" not in word: # Check if there is no letter i in the word
                    if "o" not in word: # Check if there is no letter o in the word
                        if "u" not in word: # Check if there is no letter u in the word
                            count += 1 # If no wovels found, sum 1 to the count

        # Then for every word repeat this

    return count # After the scan (loop is complete) return the integer count to the function so we can process it later

count = countwordsnowovels(par) # Execute the function passing the argument par variable as string
print(count) # Now print the result

# Note that the count variable in the function is local only, so it will not "interfere" with the one outside the loop!