Python 定义一个函数 isVowel(char),如果 char 是元音('a'、'e'、'i'、'o' 或 'u'),则返回 True,否则返回 False

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14915222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:50:24  来源:igfitidea点击:

Define a function isVowel(char) that returns True if char is a vowel ('a', 'e', 'i', 'o', or 'u'), and False otherwise

python

提问by user2066771

Question:

题:

Define a function isVowel(char) that returns True if char is a vowel ('a', 'e', 'i', 'o', or 'u'), and False otherwise. You can assume that char is a single letter of any case (ie, 'A' and 'a' are both valid).

定义一个函数 isVowel(char),如果 char 是元音('a'、'e'、'i'、'o' 或 'u'),则返回 True,否则返回 False。您可以假设 char 是任何情况下的单个字母(即,'A' 和 'a' 都是有效的)。

Do not use the keyword in. Your function should take in a single string and return a boolean.

不要使用关键字 in。你的函数应该接受一个字符串并返回一个布尔值。

Code Given:

给出的代码:

def isVowel(char):
    '''
    char: a single letter of any case

    returns: True if char is a vowel and False otherwise.
    '''

My Code:

我的代码:

def isVowel(char):
    '''
    char: a single letter of any case

    returns: True if char is a vowel and False otherwise.
    '''
    if char == 'a' or 'e' or 'i' or 'o' or 'u' or 'A' or 'E' or 'I' or 'O' or 'U':
        return True
    else:
        return False

My Problem:My output is always True. What am I doing wrong?

我的问题:我的输出总是 True。我究竟做错了什么?

采纳答案by Rohit Jain

Your if statement:

您的 if 语句:

if char == 'a' or 'e' or 'i' or 'o' or 'u' or 'A' or 'E' or 'I' or 'O' or 'U':
        return True

is equivalent to:

相当于:

if (char == 'a') or 'e' or 'i' or 'o' or 'u' or 'A' or 'E' or 'I' or 'O' or 'U':

which will always be evaluated to either True, or ewhich is also True, and hence your function always returns True.

这将始终被评估为True,或者e也为True,因此您的函数总是返回True

Change your if-statement to:

将您的 if 语句更改为:

if char == 'a' or char == 'e' or char == 'i' so on...:
        return True


But, this problem is really simple if you can use inoperator. This goes like this:

但是,如果您可以使用in运算符,这个问题真的很简单。这是这样的:

def isVowel(char):
    return char.lower() in 'aeiou'

回答by Blender

Python doesn't read like English. You'd expect your code to work, but it's evaluated like this:

Python 读起来不像英语。您希望您的代码能够正常工作,但它的评估方式如下:

if (char == 'a') or ('b') or ('c') ...

'a'is truthy (not False, 0, None, etc.), so your ifstatement will always evaluate to True.

'a'是真实的(不是False0None等),因此您的if语句将始终评估为True

To fix your code, you have to write exactlywhat you mean:

要修复您的代码,您必须准确地写出您的意思:

if char == 'a' or char == 'b' or ...

Or just use in:

或者只是使用in

if char.lower() in 'aeiou':
    ...

回答by ThiefMaster

First of all, this is how you are supposed to do it without stupid restrictions:

首先,这是你应该如何在没有愚蠢限制的情况下做到这一点:

def is_vowel(char):
    return char.lower() in 'aeiou'

Since you cannot use the inoperator. I assume the infrom the loop is allowed:

由于您不能使用in运算符。我假设in允许循环:

def is_vowel(char):
    char = char.lower()
    return any(char == c for c in 'aeiou')

If that's still a no-go, here's something that is not really nice but differs from the orchain in the other answers:

如果这仍然不行,那么这里有一些不是很好但与or其他答案中的链不同的东西:

def is_vowel(char):
    return is_in_list(char.lower(), 'aeiou')

def is_in_list(char, lst):
    if not lst:
        return False
    if char == lst[0]:
        return True
    return is_in_list(char, lst[1:])

Last but not least, you can avoid using the inoperator while still using its functionality:

最后但并非最不重要的是,您可以in在仍然使用其功能的同时避免使用运算符:

def is_vowel(char):
    return 'aeiou'.__contains__(char.lower())

Obviously this is most likely not what your professor/teacher expects - but it would show him that you are smart (or he already saw this post and will know you didn't do your homework on your own).

显然,这很可能不是您的教授/老师所期望的 - 但这会向他表明您很聪明(或者他已经看过这篇文章并且会知道您没有自己做作业)。

回答by Abraham

def is_vowel(char):
    try:
            'aeiou'.index(char.lower())
            return True
    except: 
            return False

回答by codingNEWB

I use Xcode on my Mac, but I think the results should be the same.

我在 Mac 上使用 Xcode,但我认为结果应该是一样的。

//Program: Vowels      
#include < iostream >
#include < cctype >

using namespace std;

bool isVowel (char ch);

int main()    
{
    char ch;

    cout << "Enter a letter and I will tell you if it is a vowel: \n" ;      
    cin >> ch;      
    cout << isVowel(ch) << endl;
}

bool isVowel(char ch)    
{       
    ch= tolower(ch);
    if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') {
        return true;
    }
    return false;
}

回答by dansalmo

def is_vowel(char):
    return False if 'aeiouAEIOU'.find(char) < 0 else True

回答by DEEPAK PAMWANI

def vowel(char):
    if char.lower() in 'aeiou':
        return True
    if char.upper() in 'AEIOU':
        return TRUE
    else:
        return False