提示如下:
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999 – 2002. Given variables
modelYear
andmodelName
write a statement that assignsTrue
torecalled
if the values ofmodelYear
andmodelName
match the recall details and assignsFalse
otherwise.
我写了这段代码,但总是得到错误的答案。虚假的陈述说得很好。在
modelYear >= 1999 and modelYear <= 2002
modelName == "Extravagant"
if modelYear == True and modelName == True:
recalled = True
else:
recalled = False
当你这样做的时候-
您只是丢弃了这些布尔表达式的结果,这不会神奇地生成}布尔值。在
modelYear
或{您不必尝试创建布尔值,只需使用
^{pr2}$if
块中的表达式。示例-如果在实际代码中的
if
块内,您所要做的就是分配布尔值,那么您可以-当您有:
不存储语句的结果。为了让它发挥作用,你可以这样做:
^{pr2}$但更简单的解决方案是直接在
if
语句中执行,如:最短的方法是:
recalled = modelYear >= 1999 and modelYear <= 2002 and modelName == "Extravagant"
相关问题 更多 >
编程相关推荐