Python Forum
Sentence maker help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Sentence maker help (/thread-15964.html)



Sentence maker help - bidoofis - Feb-08-2019

Okay, so recently I've been trying to make a program that makes very simple sentences. I've made a breakthrough by figuring out that 'if' and 'or' exist. Right now it looks like this:

if random.choice(ss) is 'I':
     if random.choice(an) is 'hate' or 'love':
        print("I " + random.choice(hl) + " " + random.choice(Pn))
That's all fine, but then I added another line in, so now it looks like this:
if random.choice(ss) is 'I':
     if random.choice(an) is 'hate' or 'love':
        print("I " + random.choice(hl) + " " + random.choice(Pn))
     if random.choice(an) is 'killed':
         print("I killed " + random.choice(ka))
Now, it sometimes prints both things, so it'll be like

I love people
I killed god

but sometimes it'll do just the ones from love/hate, which means that it only happens when random.choice(an) = 'killed'. How do I fix this so that only one or the other gets shown? I plan on adding another line for another random.choice(an) and I need to fix the problem so it doesn't keep outputting more than I want it to. Thanks in advance!


RE: Sentence maker help - ichabod801 - Feb-08-2019

Your code has a number of problems. First, do not use 'is' for equality. The is operator tests identity, not equality. That is, are these the same object, not are these equivalent objects. Use the equality operator (==).

Second, you are using 'or' incorrectly. This:

if random.choice(an) == 'hate' or 'love':
is equivalent to:

if (random.choice(an) == 'hate') or 'love':
Due to order of operations. Non-empty strings like 'love' are treated as True in conditional expressions. So line 2 of your code will always be True, and will always trigger. The correct way to test multiple equalities like this is the 'in' operator:

if random.choice(an) in ('hate', 'love'):



RE: Sentence maker help - bidoofis - Feb-08-2019

(Feb-08-2019, 03:36 AM)ichabod801 Wrote: Your code has a number of problems. First, do not use 'is' for equality. The is operator tests identity, not equality. That is, are these the same object, not are these equivalent objects. Use the equality operator (==). Second, you are using 'or' incorrectly. This:
 if random.choice(an) == 'hate' or 'love': 
is equivalent to:
 if (random.choice(an) == 'hate') or 'love': 
Due to order of operations. Non-empty strings like 'love' are treated as True in conditional expressions. So line 2 of your code will always be True, and will always trigger. The correct way to test multiple equalities like this is the 'in' operator:
 if random.choice(an) in ('hate', 'love'): 

Alright. I'll try this tomorrow. Thank you for helping me with this! I'm very not good at python, but I'm doing these projects to try and get better.