小编use*_*771的帖子

如何在不使用条件语句的情况下将max和min边界应用于值

问题:

写一个Python函数,clip(lo,x,hi)如果x小于lo则返回lo; 嗨如果x大于hi; 否则为x.对于这个问题,你可以假设lo <hi.

不要对此问题使用任何条件语句.相反,使用内置的Python函数min和max.在开始此问题之前,您可能希望阅读关于min的文档和max上的文档,并在解释器中稍微使用这些函数.

此函数接收三个数字并返回一个数字.

代码给出:

def clip(lo, x, hi):
    '''
    Takes in three numbers and returns a value based on the value of x.
    Returns:
     - lo, when x < lo
     - hi, when x > hi
     - x, otherwise
    '''
Run Code Online (Sandbox Code Playgroud)

我的代码已添加:

def clip(lo, x, hi):
    '''
    Takes in three numbers and returns a value based on the value of x.
    Returns:
     - lo, when x < lo
     - hi, when x > hi
     - x, otherwise …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
2
解决办法
8809
查看次数

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

题:

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

不要在中使用关键字.您的函数应该接受一个字符串并返回一个布尔值.

代码给出:

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

    returns: True if char is a vowel and False otherwise.
    '''
Run Code Online (Sandbox Code Playgroud)

我的代码:

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
Run Code Online (Sandbox Code Playgroud)

我的问题: 我的输出始终为True.我究竟做错了什么?

python

-3
推荐指数
1
解决办法
3万
查看次数

标签 统计

python ×2