Python Forum
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!
Reply
#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
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with flowchart maker program bluebouncyball 2 733 Aug-08-2023, 10:25 PM
Last Post: deanhystad
  Label Maker FPDF, Reportlab jamesaarr 1 2,604 Aug-09-2021, 11:57 PM
Last Post: Pedroski55
  while sentence kimyyya 3 2,907 Mar-20-2021, 06:00 AM
Last Post: Pedroski55
  List / arrays putting in sentence Kurta 3 2,519 Dec-25-2020, 11:29 AM
Last Post: Larz60+
  How to make a telegram bot respond to the specific word in a sentence? Metodolog 2 6,274 Dec-22-2020, 07:30 AM
Last Post: martabassof
  How to match partial sentence in long sentence Mekala 1 1,488 Jul-22-2020, 02:21 PM
Last Post: perfringo
  Remove a sentence if it contains a word. lokhtar 6 5,780 Feb-11-2020, 04:43 PM
Last Post: stullis
  Regex Help for clubbing similar sentence segments regstuff 3 2,114 Nov-20-2019, 06:46 AM
Last Post: perfringo
  how to get all the possible permutation and combination of a sentence in python sodmzs 1 4,122 Jun-13-2019, 07:02 AM
Last Post: perfringo
  wont print last sentence.. mitmit293 2 2,325 Jan-27-2019, 05:38 PM
Last Post: aakashjha001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020