此Swift(Xcode)def draw_bbox(img, bbox, labels, confidence, colors=None, write_conf=False):
global COLORS
global classes
if classes is None:
classes = populate_class_labels()
for i, label in enumerate(labels):
if colors is None:
color = COLORS[classes.index(label)]
else:
color = colors[classes.index(label)]
if write_conf:
label += ' ' + str(format(confidence[i] * 100, '.2f')) + '%'
cv2.rectangle(img, (bbox[i][0],bbox[i][1]), (bbox[i][2],bbox[i][3]), color,-1)
cv2.putText(img, label, (bbox[i][0],bbox[i][1]-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
return img
程序正确返回“ hello there”和“ cookies在哪里?”字符串。但是对于中间两个应该返回“向北!”返回“可能是任何东西”。我以为这是断点或多行字符串的问题,但这在Xcode工作场所和游乐场都发生:
ChatBot
答案 0 :(得分:2)
您的问题是使用.lowercased()-比较两个字符串是否都存在。 同样,如果您使用case语句,这个问题对我来说看起来更好:请参见下文
struct MyQuestionAnswerer {
func responseTo(question: String) -> String {
let localQuestion = question.lowercased()
question == String("where can I find the north pole?").lowercased()
let defaultNumber = question.count % 3
switch localQuestion {
case String("hello there").lowercased() : return "Why hello there"
case String("where should I go on holiday?").lowercased() : return "To the North!"
case String("where can I find the north pole?").lowercased() : return "To the North!"
case String("where are the cookies?").lowercased() : return "In the cookie jar!"
default: if (defaultNumber == 0) {return "That really depends" } else {return "Could be anything"}
}
}
}
答案 1 :(得分:2)
请阅读我在代码块中添加的注释
struct MyQuestionAnswerer {
func responseTo(question: String) -> String {
let question = question.lowercased() // <- Converts all the strings in your question into lower case
let defaultNumber = question.count % 3
if question == "hello there" {
return "Why hello there"
} else if question == "where should I go on holiday?" { // <- I is upper case
return "To the North!"
} else if question == "where can I find the north pole?" { // <- I is upper case
return "To the North!"
} else if question == "where are the cookies?" {
return "In the cookie jar!"
} else if defaultNumber == 0 {
return "That really depends"
} else if defaultNumber == 1 {
return "Ask me again tomorrow"
} else {
return "Could be anything"
}
}
}
因此,基本上,您正在将“ i ...应该在哪里”与“ I 应该在哪里”进行比较> ...”,因为此比较为假,并且与其他if块都不匹配,所以它会进入最后一个else块。