有没有办法在if语句中使用原始输入?我想问用户一个问题,如果他们输入“是”,我想编码继续在if语句之后工作。如果他们输入“否”,我希望代码说“谢谢您的时间”,然后停止该特定if语句后的所有操作。这可能吗?
代码(我之前从未这样做,所以这是一个疯狂的猜测):
tri=raw_input("Do the points that you entered form a triangle? (yes or no)")
tri=str(tri)
if tri == "yes" or "Yes" or "YES":
print "Your triangle is an:"
elif tri == "no" or "NO" or "No":
print "Thank you for your time."
else:
print "Not a valid answer, please try again later."
答案 0 :(得分:1)
试一试:
import numpy as np
import matplotlib.pyplot as plt
t = 0.0
y = 0.0
u = 0.0
F = 1.073
Wd = 2*np.pi
w0 = 1.5*Wd
b = w0/4
ts =[]
ys =[]
h= 0.001
while (t <= 30):
m1 = u
k1 = (-w0**2)*np.sin(y) + u*(-2*b) + F*(w0**2)*np.cos(Wd*t)
m2 = u + (h / 2.) * k1
t_2 = t + (h / 2.)
y_2 = y +(h / 2.) * m1
u_2 = m2
k2 = (-w0**2)*np.sin(y_2) + u_2*(-2*b) + F*(w0**2)*np.cos(Wd*t_2)
m3 = u + (h / 2.) * k2
t_3 = t + (h / 2.)
y_3 = y + (h / 2.) * m2
u_3 = m3
k3 = (-w0**2)*np.sin(y_3) + u_3*(-2*b) + F*(w0**2)*np.cos(Wd*t_3)
m4 = u + h * k3
t_4 = t + h
y_4 = y + h * m3
u_4 = m4
k4 = (-w0**2)*np.sin(y_4) + u_4*(-2*b) + F*(w0**2)*np.cos(Wd*t_4)
t = t + h
y = y + (h / 6.) * (m1 + (2 * m2) + (2 * m3) + m4)
u = u + (h / 6.) * (k1 + (2 * k2) + (2 * k3) + k4)
ts.append(t)
ys.append(y)
plt.plot(ts,ys)
plt.xlabel('$t$',fontsize=18)
plt.ylabel('$\phi$',fontsize=18)
plt.axhline(y=0, color = 'black')
plt.show()