为什么当我输入代码时:
A = 0
B = 0
while A<10 or B<5:
A+=1
B+=1
print(A)
print(B)
当B = 5时循环不会停止;而是继续将A和B都加1,直到A = 10,然后停止。当我交换订单时,会发生相同的事情,例如:
while B<5 or A<10:
当我用'and'替换'or'时,代码在B = 5时停止,这正是我想要的方式。我对“或”和“与”的实际工作方式感到困惑。
答案 0 :(得分:2)
类似于英语:
or
时, True
给出True
。
and
时, True
给出True
。
答案 1 :(得分:0)
-- Create a Temporary table for keep Maid_Id and AppointmentDateTime
CREATE TABLE #TEMP
(
Maid_Id INT,
AppointmentDateTime DATETIME
)
-- Select all AppointmentDateTime are present in Appointment
DECLARE db_cursor CURSOR FOR SELECT distinct Appointment.AppointmentDateTime FROM Appointment;
-- Iterate them to insert them into the temporary table
DECLARE @date datetime;
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO @date;
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #TEMP (Maid_Id, AppointmentDateTime)
SELECT M.Maid_Id, @date FROM Maid M
WHERE M.Maid_Id NOT IN (
SELECT a.Maid_Id FROM Appointment a
WHERE A.AppointmentDateTime = @date
)
FETCH NEXT FROM db_cursor INTO @date;
END;
CLOSE db_cursor;
DEALLOCATE db_cursor;
-- Create another temporary table to keep all the dates we need
DECLARE @MinDate DATE = '2019-05-10',
@MaxDate DATE = '2019-07-24';
CREATE TABLE #DATES
(
[DATE] DATETIME
)
-- Create a range of dates that we need and insert them in the temporary table. It can cover a wide range of dates like 2000-01-01 to Now
INSERT INTO #DATES
SELECT TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY A.OBJECT_ID) - 1, @MinDate)
FROM SYS.ALL_OBJECTS A
CROSS JOIN SYS.ALL_OBJECTS B;
-- Union all the data
SELECT M.Maid_Id, M.Maid_Name, CONVERT(NVARCHAR(10), T.AppointmentDateTime, 120) [date] FROM #TEMP T
INNER JOIN Maid M ON T.Maid_Id = M.Maid_Id
UNION
SELECT null, 'All People Are Free', CONVERT(NVARCHAR(10), [DATE], 120) FROM #DATES
ORDER BY [date]
DROP TABLE #TEMP
DROP TABLE #DATES
<10和B <A
表示当5
小于A
AND {{1} }小于10
(检查两个条件),或者换句话说,只有当B
的值大于或等于5
时,它才会停止 OR A
的值大于或等于10
(两个值中的任何一个)。
对于B
<10或B <5
,如果A
的值小于5
,则循环将继续进行 OR A
的值小于10
(两个值中的任何一个)。也就是说,如果B
和5
仍将继续循环,因为A = 10
仍满足条件。仅当B = 2
的值大于或等于10 并且 B
的值大于或等于A
时,循环才会终止(两个必须为真)。