- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我无法找到输入字符串的第一个元音的索引。当输入字符串 "elephant"、 "hello"、 "spa"时,它们可以正常工作,但是当我输入 "spam"时,它不起作用,它返回数字 3 而不是 2。我很难找到为什么它满足else 语句但不是初始 if 条件。我还试图提出一个条件,如果字符串中没有元音,那么它应该打印出字符串中最后一个字符的索引。下面是我的代码:
def find_first_vowel(word):
i = 0
while i < len(word):
i+= 1
if word[i] in vowels:
return i
else:
return len(word)-1
return i
print(find_first_vowel("spam"))
最佳答案
如果 1
位置上的字符不是元音,您的代码始终返回 len(word)-1
。另外,elephant
不起作用,spa
起作用只是因为我提到的错误,它返回 2
,即 len(word) -1
而不是找到的元音索引。尝试逐行调试代码,您很快就会弄清楚。
这可能是一个工作代码,如果没有元音,则返回-1
,否则返回找到的第一个元音的索引。
def find_first_vowel(word):
i = 0
while i < len(word):
if word[i] in vowels:
return i
i += 1
return -1
编辑
如果您想在没有元音的情况下返回最后一个字符,只需将 return -1
更改为 return len(word) - 1
即可。这里:
def find_first_vowel(word):
i = 0
while i < len(word):
if word[i] in vowels:
return i
i += 1
return len(word) - 1
关于python - 在Python中查找单词的第一个元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55010965/
我试图在 haskell 上创建一个函数,我知道该函数的定义如下: justWithA : [Char] -> Bool justWithA [] = True justWithA (x:xs) |
我试图在 haskell 上创建一个函数,我知道该函数的定义如下: justWithA : [Char] -> Bool justWithA [] = True justWithA (x:xs) |
我正在尝试计算字符串中元音的总数。我正在使用 strlen 来获取字符串的总长度,但是当我尝试按每个字母对字符串进行计数时,它说 C++ 禁止比较。所以我假设我的 if 语句有问题。 #include
Task You are given a string . It consists of alphanumeric characters, spaces and symbols(+,-). Your
我刚刚开始用 C 语言编程,我必须创建一个程序来计算字符串有多少个元音。到目前为止我有这个: int a; int len = strlen(text)-1 for(a=0;a==len;++a){
尝试学习 Python 中的正则表达式以查找具有连续元音-辅音或辅音-元音组合的单词。我将如何在正则表达式中执行此操作?如果无法在 Regex 中完成,是否有一种在 Python 中执行此操作的有效方
我想输入 dagger-alif、dagger-waw 和 dagger-ya(也称为微型 alif、微型哇和微型 ya)作为 alif-wasla 使用 PC 阿拉伯语键盘。这些标记被使用帮助读者发
我正在尝试创建一个将文件上传到 FTP 服务器的批处理文件。除了一个特定文件夹的名称中包含突变/元音外,一切正常(无法更改。也就是文件夹名称中包含 ö。)。 我的问题是:有哪些选择可以实现这一目标?
我是一名优秀的程序员,十分优秀!