Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Sentence maker help
#1
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!
Quote
#2
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'):
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to get all the possible permutation and combination of a sentence in python sodmzs 1 177 Jun-13-2019, 07:02 AM
Last Post: perfringo
  wont print last sentence.. mitmit293 2 244 Jan-27-2019, 05:38 PM
Last Post: aakashjha001
  Generating all simple sentence possibilities Max_77 3 547 Oct-10-2018, 11:35 AM
Last Post: Max_77
  Python - Limit Sentence Length to 10 Words - Text file dj99 2 994 Jul-21-2018, 02:24 PM
Last Post: dj99
  PigLatin Sentence Translator Help 2skywalkers 7 1,278 Jun-23-2018, 12:46 AM
Last Post: 2skywalkers
  Help with image maker program SteampunkMaverick12 2 639 Apr-23-2018, 07:00 PM
Last Post: SteampunkMaverick12
  Is there py code that splits an audio containing speech per every sentence? infuj02 1 705 Mar-07-2018, 04:30 PM
Last Post: Larz60+
  weird error in random sentence generator bobger 9 1,441 Nov-29-2017, 07:34 PM
Last Post: bobger

Forum Jump:


Users browsing this thread: 1 Guest(s)