我试图写一个简单的if语句,它取决于一组条件(列表中出现的是0)但是当我的两个条件都是0时,我遇到了错误,只有一个显示语句。
if let1 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",a, "\n")
elif let2 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",b, "\n")
elif let3 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",c, "\n")
我已尝试使用或声明,但无法产生所需的输出,通知用户他们的输入中的一个,两个或三个可能不在我程序的其余部分中使用的文本正文中。
对于缺乏succint术语而道歉,因为我对编程很陌生。
由于
编辑:感谢您的回复,但我应该给予全部功能......def segment_sequence(mycorpus, letter1, letter2, letter3):
corpus_string = "".join(mycorpus)
a = letter1
b = letter2
c = letter3
d = a + b
e = b + c
let1 = corpus_string.count(a)
let2 = corpus_string.count(b)
let3 = corpus_string.count(c)
let1let2 = corpus_string.count(d)
let2let3 = corpus_string.count(e)
print(let1,let2,let3)
if let1 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",a, "\n")
if let2 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",b, "\n")
if let3 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",c, "\n")
break
else:
trans_prob1 = let1let2/let1
trans_prob2 = let2let3/let2
if trans_prob1 > trans_prob2:
print('Here is the proposed boundary given the training corpus:\n')
print('Proposed end of one word:' ,d,'\n')
print('Proposed beginning of new word:' ,c,'\n')
elif trans_prob2 > trans_prob1:
print('Here is the proposed boundary given the training corpus:\n')
print('Proposed end of one word: ',e,'\n')
print('Proposed beginning of new word: ',a,'\n')
elif trans_prob1 == trans_prob2:
print('Both ',d,' and ',e,' are equally possible word endings')
return()
使用if而不是elif的问题是它没有中断,然后我得到除以0的错误。我没有找到任何过于复杂的简单而准确的用户错误消息。
再次感谢您
答案 0 :(得分:1)
使用elif
尝试使用if
,如下所示:
if let1 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",a, "\n")
if let2 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",b, "\n")
if let3 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",c, "\n")
答案 1 :(得分:0)
如果您希望针对每种情况测试条件,请不要使用elif:
此外,为了确保您的功能在触发案例时中止,这会导致除数为零,我在测试电池后添加了一个标志和一个测试以退出。
def segment_sequence(mycorpus, letter1, letter2, letter3):
corpus_string = "".join(mycorpus)
a = letter1
b = letter2
c = letter3
d = a + b
e = b + c
let1 = corpus_string.count(a)
let2 = corpus_string.count(b)
let3 = corpus_string.count(c)
let1let2 = corpus_string.count(d)
let2let3 = corpus_string.count(e)
print(let1,let2,let3)
must_abort = False
if let1 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",a, "\n")
must_abort = True
if let2 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",b, "\n")
must_abort = True
if let3 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",c, "\n")
must_abort = True
if must_abort:
return
# no need for else any longer here.
trans_prob1 = let1let2/let1
trans_prob2 = let2let3/let2
if trans_prob1 > trans_prob2:
print('Here is the proposed boundary given the training corpus:\n')
print('Proposed end of one word:' ,d,'\n')
print('Proposed beginning of new word:' ,c,'\n')
elif trans_prob2 > trans_prob1:
print('Here is the proposed boundary given the training corpus:\n')
print('Proposed end of one word: ',e,'\n')
print('Proposed beginning of new word: ',a,'\n')
elif trans_prob1 == trans_prob2:
print('Both ',d,' and ',e,' are equally possible word endings')
return
现在,无论先前的条件是否得到满足,每个案例都会被评估并采取行动。
def segment_sequence(mycorpus, letter1, letter2, letter3):
corpus_string = "".join(mycorpus)
a = letter1
b = letter2
c = letter3
d = a + b
e = b + c
let1 = corpus_string.count(a)
let2 = corpus_string.count(b)
let3 = corpus_string.count(c)
let1let2 = corpus_string.count(d)
let2let3 = corpus_string.count(e)
print(let1,let2,let3)
if must_abort(let1, let2, let3):
return
trans_prob1 = let1let2/let1
trans_prob2 = let2let3/let2
if trans_prob1 > trans_prob2:
print('Here is the proposed boundary given the training corpus:\n')
print('Proposed end of one word:' ,d,'\n')
print('Proposed beginning of new word:' ,c,'\n')
elif trans_prob2 > trans_prob1:
print('Here is the proposed boundary given the training corpus:\n')
print('Proposed end of one word: ',e,'\n')
print('Proposed beginning of new word: ',a,'\n')
elif trans_prob1 == trans_prob2:
print('Both ',d,' and ',e,' are equally possible word endings')
return
def must_abort(let1, let2, let3):
abort = False
if let1 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",a, "\n")
abort = True
if let2 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",b, "\n")
abort = True
if let3 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",c, "\n")
abort = True
return abort
答案 2 :(得分:0)
如果elif的工作方式如此,它会检查条件会在哪里获得真实状态,之后就会破坏。所以你必须使用if。
if let1 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",a, "\n")
if let2 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",b, "\n")
if let3 == 0:
print("Segmentation unavailable as corpus does not contain the following letter ",c, "\n")