我有一个变量player_answer
,我希望在player_answer
等于“是”或“是”时打破while循环。
代码:
while player_answer != "yes" ((OR)) "yea":
*CODE BLOCK*
答案 0 :(得分:2)
如果你想使用两个子句,你可以使用import pandas as pd
from pandas import Series,DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import csv
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import train_test_split
from sklearn import metrics
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
trainDF = pd.DataFrame({'Out': [1, 0, 0, 0, 1, 3, 3],
'Revolver': [0.766, 0.957, 0.658, 0.233, 0.907, 0.1, 0.15],
'Ratio': [0.803, 0.121, 0.085, 0.036, 0.024, 0.6, 0.8],
'Num': [0, 1, 0, 3, 5, 4, 4]})
#drop NA values
trainDF = trainDF.dropna()
trainDF['Num'].loc[(trainDF['Num']==8) | (trainDF['Num']==17)] = trainDF['Num'].median()
# convert dataframe to numpy array
y = trainDF['Out'].as_matrix()
# convert dataframe to numpy array
X = trainDF.drop('Out', 1).as_matrix()
target_names = ['out', 'in']
pca = PCA(n_components=2)
X_r = pca.fit(X).transform(X)
lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(X, y).transform(X)
# Percentage of variance explained for each components
print('explained variance ratio (first two components): %s'
% str(pca.explained_variance_ratio_))
plt.figure()
for c, i, target_name in zip("rgb", [0, 1], target_names):
plt.scatter(X_r[y == i, 0], X_r[y == i, 1], c=c, label=target_name)
plt.legend()
plt.title('PCA of Out')
plt.figure()
for c, i, target_name in zip("rgb", [0, 1], target_names):
plt.scatter(X_r2[y == i, 0], X_r2[y == i, 1], c=c, label=target_name)
plt.legend()
plt.title('LDA of Out')
plt.show()
/ and
来表示python复合逻辑。
or
然而,要小心,因为复合条款的否定并不像上面描述的那样简单,因此容易混淆。具体来说,我写的化合物 NOT 等同于
while not (player_answer == "yes" or player_answer == "yea"):
<INSERT CODE>
有关详细信息,请查看DeMorgan's Laws。
更好的解决方案是评论中来自@DeveloperXY的解决方案:
# DON'T DO THIS; THIS ILLUSTRATES A COMMON LOGICAL MISTAKE
while player_answer != "yes" or player_answer != "yea":
<INSERT CODE>