Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 布尔表达式示例_Python_Python 2.7_Python 3.x_Boolean - Fatal编程技术网

Python 布尔表达式示例

Python 布尔表达式示例,python,python-2.7,python-3.x,boolean,Python,Python 2.7,Python 3.x,Boolean,给定变量model、year和make,编写一个布尔值 如果make包含值,则计算结果为True的表达式 'ford'和年份大于2013或者如果车型有效 “野马”和年份大于或等于2012,False 否则 我对如何在表达式中表示“contains”和“hold”感到困惑 我的答覆是: if make == 'ford' and year > 2013: return True elif model >= 'mustang' and year >= 2012: ret

给定变量
model
year
make
,编写一个布尔值 如果
make
包含值,则计算结果为
True
的表达式
'ford'
年份
大于
2013
或者如果
车型
有效
“野马”
年份
大于或等于
2012
False
否则

我对如何在表达式中表示“contains”和“hold”感到困惑

我的答覆是:

if make == 'ford' and year > 2013:
    return True
elif model >= 'mustang' and year >= 2012:
  return True
else False

看起来有一些简单的错误,我想这就是你想要的。“包含”和“保持”,我相信,只是指变量

make = "ford"
model = "mustang"
year = 2012

def cars(make, model, year):
    if make == 'ford' and year > 2013:
        return True
    elif model == 'mustang' and year >= 2012:
        return True
    else:
        return False

请完成(或至少尝试)你自己的家庭作业。把它分成两部分。首先,给自己找一个福特2013标准的表达式。那就给野马买一辆。把每一个放在括号中,然后加上一个“or”。[你需要填写“and”和“or”在Python中的表示方式]那么你的答案呢?是什么让你得到了这个答案,为什么你认为它可能是错的?因此,如果您需要学习支持,那么这不是一项辅导服务,请与相关教授/助教联系。看起来您需要model==‘mustang’。此外,这里的“包含”一词是不明确的。所以,就像模型是“福特温莎”一样,不清楚预期的行为是什么。如果这不算“福特”,你的代码就接近了。如果要计算,则需要检查“ford”是否为子字符串。此外,您可能需要注意,如果我们通过“福特”的大写字母F,会发生什么。非常感谢!