为什么我的三元忽略了第一个条件($order->status === "prepairing")
?
在检查订单状态时,它总是跳过第一个条件,并立即转到第二个条件(并且总是将其视为真)
$messageMiddle = ( ($order->status === "prepairing") ? " your prder is being prepared. Make your way towards the store."
: ($order->status === "complete") ?' your order is done! Please show your order-code at the store.'
: ' thank you for ordering ');
答案 0 :(得分:1)
您需要按如下所示将括号中的每个下一个表达式分组。您忘了将第二元表达式用括号括起来。
Vx0 = 18
Vy0 = 18
V = 18
Theta = 45
y0 = 1.8
x0 = 0
p = 1.2#given density
C = 0.3#Drag coefficient
dt = 0.2#Time increment
def x_direction (x0, Vx0, dt):
"""Calculate the distance moved in x axis"""
new_dist = 0
for hori_dist in range(5):
hori_dist = x0 + Vx0*dt
new_dist = new_dist + hori_dist
return new_dist
new_dist = x_direction(x0, Vx0, dt)
print ("Distanced moved in x direction is :", new_dist)
但是您还是应该避免这种方法。
答案 1 :(得分:1)
对订单状态做出反应的更好方法是使用switch statement。像这样:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
var text = Request.Params[0];
XDocument xdoc = XDocument.Load(Server.MapPath("~/cours.xml"));
XDocument xresp = new XDocument(new XElement("tousCours"));
if (text != "")
{
IEnumerable<XElement> prog =
from b in xdoc.Elements("tousCours").Elements("cours")
where (string)b.Element("prgs/pr") == text
select b;
foreach (XElement xEle in prog)
xresp.Element("tousCours").Add(xEle);
}
Response.Clear();
Response.ContentType = "text/xml";
Response.ContentEncoding = System.Text.Encoding.UTF8;
xresp.Save(Response.Output);
Response.End();
很容易看出如何扩展它以对其他状态字做出反应。
请注意,我将“ prepairing”更改为“ preparing”。
程序员追求的目标之一就是简洁的代码。但是,较短的代码并不总是更好的代码。它的可读性较低,维护和扩展起来也更加困难。