简体   繁体   English

基于多个变量分配真/假变量

[英]assign true/false variable based on multple variables

Here is my assignment:这是我的任务:

Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006.Given a variable modelYear write a statement that assigns True to recalled if the value of modelYear falls within the two recall ranges and assigns False otherwise. Clunker Motors Inc. 正在召回 1995-1998 年和 2004-2006 年的所有车辆。给定一个变量 modelYear,写一个语句,如果 modelYear 的值落在两个召回范围内,则将 True 分配给召回,否则分配 False。

Do not use an if statement in this exercise!不要在本练习中使用 if 语句!

so if I'm reading this correctly:因此,如果我正确阅读此内容:

I need to have a variable "recalled" that will be assigned true or false based on the condition of the variable modelYear being between 1995 and 1998 OR being between 2004 and 2006.我需要有一个变量“召回”,它将根据变量 modelYear 在 1995 年和 1998 年之间或在 2004 年和 2006 年之间的条件分配为真或假。

In doing some searching I found this segment of code someone else used for this challenge:在进行一些搜索时,我发现了其他人用于此挑战的这段代码:

recalled = ((modelYear >= 1995) And ((modelYear <= 1998)) Or (modelYear >=2004) And (modelYear <=2006))

Now MyProgrammingLab is not accepting this as a valid answer to the situation.现在 MyProgrammingLab 不接受这是对这种情况的有效答案。 It say SyntaxError line 1. Am I just overlooking an open parenthesis or missing a capitalization or something?它说 SyntaxError 第 1 行。我是否只是忽略了一个左括号或缺少大写或其他什么? Or am I completely wrong in using this approach?还是我使用这种方法完全错误?

You are quite close, but you are using the wrong tools.您非常接近,但是您使用了错误的工具。 I use PyCharm , and run the code locally.我使用PyCharm ,并在本地运行代码。 There are other IDEs which are excellent, but this is the one I use.还有其他优秀的 IDE,但这是我使用的一个。 When I typed in this:当我输入这个时:

modelYear = 1997
recalled = ((modelYear >= 1995) And ((modelYear <= 1998)) Or (modelYear >=2004) And (modelYear <=2006))

PyCharm showed me: PyCharm 向我展示了:

在此处输入图片说明

Note the red underline for the syntax error, and when the cursor is hovered, it says 'Unresolved reference' because it does not know what And is.请注意语法错误的红色下划线,当光标悬停时,它会显示“未解析的引用”,因为它不知道And是什么。 It should be and .应该是and

Better Tools == Better Code.更好的工具 == 更好的代码。

Cheers and good luck.干杯,祝你好运。

Remember PEP8 rules which says,special reserved keywords are in lowercase,so its not And,Or but and,or记住 PEP8 规则说,特殊保留关键字是小写的,所以它不是And,Or but and,or

click on pythonrules点击python规则

Pep8 Rules Pep8 规则

Run your code locally - figure out your SyntaxError.在本地运行您的代码 - 找出您的 SyntaxError。 Read the examples more carefully.更仔细地阅读示例。 And / Or are not capitalized.和/或不大写。

Also, Python has a range function.另外,Python 有一个 range 函数。 You could use that instead你可以用它代替

recalled = modelYear in range(1995, 1999) or modelYear in range(2004, 2007) 

You use end year +1 because ranges are non-inclusive您使用结束年份 +1,因为范围不包括在内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM