Python 为什么可以';我用三个if语句代替一个if语句和两个elif语句吗?
我正在努力理解Python 为什么可以';我用三个if语句代替一个if语句和两个elif语句吗?,python,if-statement,Python,If Statement,我正在努力理解elif在特定代码段中的用法 在下面的复制粘贴代码中,一旦进入while循环,就有一个if语句(如果activityToday==“Sleep”)和两个elif语句(如果activityToday==“Run”或activityToday==“Icecream”分别输入) 如果我运行代码,我会得到预期的输出: Start state: Run Possible states: ['Run', 'Icecream', 'Run'] End state after 2 days: Ru
elif
在特定代码段中的用法
在下面的复制粘贴代码中,一旦进入while
循环,就有一个if
语句(如果activityToday==“Sleep”
)和两个elif
语句(如果activityToday==“Run”
或activityToday==“Icecream”
分别输入)
如果我运行代码,我会得到预期的输出:
Start state: Run
Possible states: ['Run', 'Icecream', 'Run']
End state after 2 days: Run
Probability of the possible sequence of states: 0.21
Start state: Run
Possible states: ['Run', 'Run', 'Run']
End state after 2 days: Run
Probability of the possible sequence of states: 0.25
如果我将两个elif
语句更改为两个If
语句,我会得到一些意外的输出:
Start state: Run
Possible states: ['Run', 'Icecream', 'Run']
End state after 2 days: Run
Probability of the possible sequence of states: 0.21
Start state: Run
Possible states: ['Run', 'Run', 'Run']
End state after 2 days: Run
Probability of the possible sequence of states: 0.25
问题是我不理解意外的输出我不明白为什么我不能用两个if
语句替换两个elif
语句,并将它们作为三个独立的循环运行,而不是用一个if
和两个elif
来运行一个循环。有人能给我解释一下吗
谢谢你的帮助
import numpy as np
import random as rm
# The statespace
states = ["Sleep","Icecream","Run"]
# Possible sequences of events
transitionName = [["SS","SR","SI"],["RS","RR","RI"],["IS","IR","II"]]
# Probabilities matrix (transition matrix)
transitionMatrix = [[0.2,0.6,0.2],[0.1,0.6,0.3],[0.2,0.7,0.1]]
# FROM THE TUTORIAL
# A function that implements the Markov model to forecast the state/mood.
def activity_forecast(days):
# Choose the starting state
activityToday = "Run"
print("Start state: " + activityToday)
# Shall store the sequence of states taken. So, this only has the starting state for now.
activityList = [activityToday]
i = 0
# To calculate the probability of the activityList
prob = 1
while i != days:
if activityToday == "Sleep":
change = np.random.choice(transitionName[0],replace=True,p=transitionMatrix[0])
if change == "SS":
prob = prob * 0.2
activityList.append("Sleep")
pass
elif change == "SR":
prob = prob * 0.6
activityToday = "Run"
activityList.append("Run")
else:
prob = prob * 0.2
activityToday = "Icecream"
activityList.append("Icecream")
elif activityToday == "Run": # WHY CAN'T I CHANGE 'ELIF' TO 'IF' ??????
change = np.random.choice(transitionName[1],replace=True,p=transitionMatrix[1])
if change == "RR":
prob = prob * 0.5
activityList.append("Run")
pass
elif change == "RS":
prob = prob * 0.2
activityToday = "Sleep"
activityList.append("Sleep")
else:
prob = prob * 0.3
activityToday = "Icecream"
activityList.append("Icecream")
elif activityToday == "Icecream": # WHY CAN'T I CHANGE 'ELIF' TO 'IF' ??????
change = np.random.choice(transitionName[2],replace=True,p=transitionMatrix[2])
if change == "II":
prob = prob * 0.1
activityList.append("Icecream")
pass
elif change == "IS":
prob = prob * 0.2
activityToday = "Sleep"
activityList.append("Sleep")
else:
prob = prob * 0.7
activityToday = "Run"
activityList.append("Run")
i += 1
print("Possible states: " + str(activityList))
print("End state after "+ str(days) + " days: " + activityToday)
print("Probability of the possible sequence of states: " + str(prob))
# Function that forecasts the possible state for the next 2 days
activity_forecast(2)
您无法执行此操作,因为您更改了
if
/elif
语句中的activityToday
的值。因此,如果没有elif
,当循环时,两个条件块可以在的一次迭代中运行。如果如果与elif
相同,为什么您认为需要elif
elif
是else
+if
。您正在更改if的每个分支内的activityToday
值(即条件)。因此,在一次循环迭代中,它可能会得到多次更改如果x==1:x=2如果x==2:x=3,则x=1与如果x==1:x=2如果x==2:x=3,则x=1不相同`