Khoa Nguyen
Khoa Nguyen

Reputation: 91

Python IF conditions

I'm new to django python so the if makes me feel confused. Please help me!

Im trying to make a IF condition for Facebook chat bot API. The whole function is when a user sends something in the Facebook chat, my bot will remove all punctuations, lower case the text and split it based on space. After that he will pick a joke from json list with the keyword he got. A quick reply with 2 buttons "Yes" and "No" is also sent to user with the joke.

It also checks if the keyword is absent. Now I want that when user types "No" in the chat, my bot will send something back like "Then not". What did I do wrong here?

strong texttokens = re.sub(r"[^a-zA-Z0-9\s]", ' ', recevied_message).lower().split()
joke_text = ''
for token in tokens:
    if token in jokes:
        joke_text = random.choice(jokes[token])

        .... some code

        send_message(fbid, joke_text)

        quick_text = "Do you want another joke? "
        send_quick_reply_message(fbid, quick_text)
        break
if not joke_text:
    joke_text = "I didn't understand! " \
                "Send 'stupid', 'fat', 'dumb' for a Yo Mama joke!"
    send_message(fbid, joke_text)
    if 'No':
        joke_text = "Then not"
        send_message(fbid, joke_text)

Upvotes: 0

Views: 130

Answers (3)

Jack Cai
Jack Cai

Reputation: 3

'No' is a string.

if 'No'  

always return True.

Upvotes: 0

Razik
Razik

Reputation: 182

I think this code have a problem

 if 'No':
    joke_text = "Then not"
    send_message(fbid, joke_text)

What is 'No"? You have to compare something with "No". And also this if condition is on wrong indentation level.

Upvotes: 0

iScrE4m
iScrE4m

Reputation: 882

if 'No': 

is always true, you probably want to be checking if something == 'No'?

Upvotes: 4

Related Questions