# lab\_sheet\_8\_zh-hans

Written by:

<figure><img src="https://avatars.githubusercontent.com/u/60097976?s=400&#x26;u=46206cff80830b3e6e2d04154b9c75a0a156c056&#x26;v=4" alt="" width="188"><figcaption><p>Binary-Yuki</p></figcaption></figure>

<figure><img src="https://avatars.githubusercontent.com/u/146884379?v=4" alt="" width="188"><figcaption><p>UniDesiranCX</p></figcaption></figure>

在Python里 逻辑运算符（and or not）和比较运算符（== != > <等）是两个不同的概念

(p.s. 这几个比较常见)

逻辑运算符用于连接多个比较运算符得到的布尔值 得到一个最终的布尔值

例如：

* and：两边都为真（True） 结果为真（True）；否则为假（False）
* or：两边至少有一真（True） 结果为真（True）；否则为假（False）
* not：真（True）变为假（False） 假（False）变为真（True）

比较运算符用于比较两个值的关系 返回一个布尔值 例如等于（==）判断两边的值是否相等

如果

* 相等返回True
* 否则为False
* 不等于（!=）则相反

```txt
II. Suppose the value of b is False and value of x is 0. What is the value of each of the following expressions? 

a. b and x == 0 b. 

b or x == 0 

c. not b and x == 0 

d. not b or x != 0
```

题目给出的变量b的值为False 变量x的值为0

```python
解题代码a
b and x == 0
b = False
x = 0
result = b and x == 0
print(result)  # 输出：False
```

这里面x == 0 为 True 然后再对 `b` 和这个 `True` 值进行逻辑与运算 `False and True` 的结果为 `False`

```python
解题代码b
b or x == 0
result = b or x == 0
print(result)  # 输出：True
```

x == 0 为 True 然后再对 b 和这个 True 值进行逻辑`或`运算 `False or True` 的结果为`True`

```python
not b and x == 0
result = not b and x == 0
print(result)  # 输出：True
```

首先计算 `not b b` 为 `False not False` 的结果是 `True` 再对 `True` 和 `x == 0`（结果为 True）进行逻辑与 `True and True` 的结果是`True`

```python
not b or x != 0
result = not b or x != 0
print(result)  # 输出：True
```

首先计算`not b b`为 `False not False` 的结果是`True`再对 `True` 和 `x != 0`（结果为 False）进行逻辑`或` `True or False` 的结果是 `True`

```txt
III.Simplify the following expressions. Assume the variable b is a boolean variable.
a. b == True
b. b == False
c. b != True
d. b != False
```

这四个问题都可以通过Python的逻辑运算符进行简化

首先b == True和b实际上是一个意思

b == False对应的是not b 相当于否定的b

a) `b == True`

这个表达式直接可以简化为 `b` 因为如果 `b`是`真`那么 `b == True` 就返回`True` 否则返回`False` 等价于 `b` 本身

b) `b == False`

可以简化为 `not b` 因为`b == False` 在 `b` 为真的情况下返回 `False` 在`b`为假的情况下返回 `True` 与 `not b` 的逻辑相同

c) `b != True`

可以简化为 `not b` 它实际上是在描述 `b 不是 True` 也就是 `b 是 False` 这与 `not b` 的定义相同

d.) `b != False`

可以简化为 `b` `b != False`描述的是 `b` 不是 `False`也就是 `b` 是 `True` 这与 `b` 的定义相同

所以这四个表达式可以简化为：

a. `b` b. `not b` c. `not b` d. `b`

```txt
IIII. Write a program that asks the user to enter a month (1 for January, 2 for February, and
so on) and then prints the number of days in the month. For February, print ‘28 or 29 days’.
Sample output:
Enter a month: 5
30 days
```

这是一个简单的基于用户输入来判断月份天数的程序 可以使用Python的 if-elif-else 语句来完成

首先 需要用户输入一个代表月份的数字 然后根据这个数字判断月份有多少天 有两种情况需要特殊对待：二月份（可能是28或29天）和无效的输入（输入的数字不在1到12之间）

```python
# 获取用户输入的月份
month = int(input("请输入一个月份（1代表一月 2代表二月 等等）："))

# 判断输入的月份对应的天数
if month == 2:  # 如果是二月就是28/29天
    print('28或29天')
elif month in [1, 3, 5, 7, 8, 10, 12]: 
    print('31天')
elif month in [4, 6, 9, 11]:
    print('30天')
else:
    print('输入无效！请输入1到12的整数 ')

```

首先获取用户输入并转为整数类型 然后根据输入的数字判断并输出对应的天数 如果输入的数字不符合要求 就报错

```txt
IIIII. A year with 366 days is called a leap year. Leap years are necessary to keep the calendar synchronized with the sun because the earth revolves around the sun once every
365.25 days. Actually, that figure is not entirely precise, and for all dates after 1582 the
Gregorian correction applies. Usually years that are divisible by 4 are leap years (for
example, 1996). However, years that are divisible by 100 (for example, 1900) are not
leap years but years that are divisible by 400 are leap years (for example, 2000). Write
a program that asks the user for a year and computes whether that year is a leap year.
Use a single if-statement and logic operators.
```

首先解题思路是：

```mermaid
graph TD
A[year能被4整除] -->|是| B[year不能被100整除]
A -->|否| D[不是闰年]
B -->|是| C[是闰年]
B -->|否| E[year能被400整除]
E -->|是| F[是闰年]
E -->|否| G[不是闰年]
```

代码：

```python
# 获取用户输入
year = int(input("请输入一个年份："))

# 判断是否为闰年
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    print(year, "是闰年")
else:
    print(year, "不是闰年")
```

代码解析：

* `year % 4 == 0`：用输入的年份 `year` 对4取余数 如果余数为0 就表示这个年份能被4整除
* `year % 100 != 0`：用输入的年份 `year` 对100取余数 如果余数不为0 就表示这个年份不能被100整除
* `year % 400 == 0`：用输入的年份 `year` 对400取余数 如果余数为0 就表示这个年份能被400整除

`and` 逻辑操作符用在 `year % 4 == 0 and year % 100 != 0` 部分 表示这两个判断条件都必须为真 结果才为真 也就是说 如果年份能被4整除且不能被100整除 这部分的结果就为真

`or` 逻辑操作符用在整体判断语句 `(year % 4 == 0 and year % 100 != 0) or year % 400 == 0` 部分 表示只要这两部分中的任一部分为真 整个判断结果就为真 也就是说 只要年份能被4整除且不能被100整除 或者能被400整除 就认定这个年份为闰年

解体思路：

* 解析输入字符串 提取出每个开关的状态以及档位设置
* 初始化左右门的状态为关闭
* 判断主解锁开关是否打开且档位是否处于停车位置
* 如果这两个条件都满足 就可以继续判断车门是否可以打开 否则 两扇门都保持关闭
* 如果第3步的条件满足 接着判断左侧门的开启条件：
* * 如果左侧门仪表盘开关打开 或者左侧门外部手柄打开 或者儿童锁关闭且左侧门内部手柄打开 那么左侧门就可以打开
* 同样判断右侧门的开启条件：
* * 如果右侧门仪表盘开关打开 或者右侧门外部手柄打开 或者儿童锁关闭且右侧门内部手柄打开 那么右侧门就可以打开
* 最后 根据左右门的状态返回对应的信息
* * 如果左右门都打开 就返回“两扇门都打开”
* * 如果只有左侧门打开 就返回“左侧门打开”
* * 如果只有右侧门打开 就返回“右侧门打开”
* * 如果两扇门都没有打开 就返回“两扇门都保持关闭”

```python
def minivan_door_status(input_str):
    # 从输入字符串中提取仪表盘开关状态
    dashboard = input_str[:4]
    # 从输入字符串中提取门把手状态
    door_handles = input_str[4:8]
    # 从输入字符串中提取档位状态
    gear_shift = input_str[8]

    # 将仪表盘开关状态转换为整数
    left_door_dashboard_switch = int(dashboard[0])  # 左侧门仪表盘开关状态
    right_door_dashboard_switch = int(dashboard[1])  # 右侧门仪表盘开关状态
    child_lock_switch = int(dashboard[2])  # 儿童锁开关状态
    master_unlock_switch = int(dashboard[3])  # 主解锁开关状态

    # 将门把手状态转换为整数
    left_door_inside_handle = int(door_handles[0])  # 左侧门内部手柄状态
    left_door_outside_handle = int(door_handles[1])  # 左侧门外部手柄状态
    right_door_inside_handle = int(door_handles[2])  # 右侧门内部手柄状态
    right_door_outside_handle = int(door_handles[3])  # 右侧门外部手柄状态

    # 初始化门的状态为关闭
    left_door_opens = False
    right_door_opens = False

    # 如果主解锁开关打开并且档位处于停车位置
    if master_unlock_switch == 1 and gear_shift == 'P':  # 判断主解锁开关是否打开 以及档位是否在停车位置
        # 如果左侧门仪表盘开关打开 或者左侧门外部手柄打开 或者儿童锁关闭且左侧门内部手柄打开
        if left_door_dashboard_switch == 1 or (left_door_outside_handle == 1 or (left_door_inside_handle == 1 and child_lock_switch == 0)):  # 判断左侧门的开启条件
            left_door_opens = True  # 左侧门打开
        # 如果右侧门仪表盘开关打开 或者右侧门外部手柄打开 或者儿童锁关闭且右侧门内部手柄打开
        if right_door_dashboard_switch == 1 or (right_door_outside_handle == 1 or (right_door_inside_handle == 1 and child_lock_switch == 0)):  
# 判断右侧门的开启条件
            right_door_opens = True  # 右侧门打开

    # 根据门的状态返回对应的信息
    if left_door_opens and right_door_opens:  # 如果左侧门和右侧门都打开
        return "Both doors open."  # 返回 两扇门都打开
    elif left_door_opens:  # 如果只有左侧门打开
        return "Left door opens."  # 返回 左侧门打开
    elif right_door_opens:  # 如果只有右侧门打开
        return "Right door opens."  # 返回 右侧门打开
    else:  # 如果两扇门都没有打开
        return "Both doors stay closed."  # 返回 两扇门都保持关闭
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.tzpro.xyz/kif-notes/lab_sheet_8_zh-hans.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
