我想這個函數去測試3個語句並返回適當的響應。這三個聲明是和他們預期的反應是python需要幫助if函數
「」 - 有反應「空白空間,
‘你好’ - 有迴應‘一個字’,
‘世界’ - 有反應」另一個字「
問題是'世界'也給予反應」單個單詞「,而不是」單個單詞再次「。有沒有什麼辦法讓python檢測到以前的反應是」單個單詞「,所以它如果在第一個單詞之後輸入另一單個單詞,會再次聲明「單個單詞」?
def return_statement(statement):
if statement == (''):
response = "blank space"
return response
if ' ' not in statement
response = "single word"
return response
if ' ' not in statement and response == "single word":
response = 'single word again'
return response
除了事物的答案中指出below..I認爲你是缺少「:」第二個if語句.. – prashantsunkari
你描述你的問題的方式似乎與你的要求相矛盾。 *'世界'*是否會返回*'另一個詞'*或*'單一的世界'*? – nqngo