我在if
/else
條件中使用while
循環。出於某種原因,在一種情況下while循環不起作用。條件顯示在我的代碼下面。在這些條件下,我會假定應該使用else
條件,並且應該減少weight
和max_speed
,直到while
條件不再有效。我究竟做錯了什麼?Python while循環無法正常工作
weight = 0
max_speed = 15
if weight == 0 and max_speed <= 10:
while weight == 0 and max_speed <= 10:
weight=weight+1
print(weight)
print(max_speed)
else:
while weight != 0 and max_speed > 10:
weight = weight-1
max_speed=max_speed-1
print(weight)
print(max_speed)
用的是什麼,而在這裏循環?我不能理解它背後的邏輯。你想實現什麼目標? –
它進入'else'分支,但由於weight爲'0',因此'weight!= 0'的計算結果爲'False',因此整個'weight!= 0和max_speed> 10'表達式的計算結果爲'False'。這就是爲什麼while循環不能運行的原因。 – Sevanteri
你想要'weight = 0'和'max_speed = 10'嗎? – jbsu32