显然,我的字符串识别算法无法正常工作。它根据用户的输入返回错误的响应。我知道它可能很简单,但我现在无法看到它是什么。我完成了研究,但我还没有找到另一个python问题,当输入用户输入时,返回错误的响应。我的if语句是否正确形成?字符串搜索有问题吗?
import random
def po_response():
response = {1 : 'This is random response A from po_response()',
2 : 'This is random response B from po_response()',
3 : 'This is randomw response C from po_response()'}
answer = random.sample(response.items(),1)
print(answer)
main()
def greetings_response():
response = {1 : 'This is response A from greetings_response()',
2 : 'This is response B from greetings_response()',
3 : 'This is response C from greetings_response()'}
answer = random.sample(response.items(),1)
print(answer)
return response
main()
def main():
userRequest = input('Welcome, Please enter your request. \n')
userReq = userRequest.lower()
if 'how are you' or 'how\'s it going' in userReq:
print('first if')
print('The value of the input is ')
print(userReq)
greetings_response()
elif 'ship' + 'po' or 'purchase order' in userReq:
print('elif')
print('The value of the input is ')
print(userReq)
po_response()
else:
print('Please re-enter your request')
main()
以下是我输入po'
时的回复 Welcome, Please enter your request.
>>>ship po
first if
The value of the input is
ship po
[(2, 'This is response B from greetings_response()')]
它不应该转到greetings_response()函数,它应该转到po_response()函数。不知道为什么它会以这种方式行事。
答案 0 :(得分:1)
使用or
进行测试似乎有误。也许你的意思是if ('how are you' in userReq) or ('how\'s it goind' in userReq)
?看看Python运营商的优先级。