Python Forum
if statement and in operator problem - 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: if statement and in operator problem (/thread-6608.html)



if statement and in operator problem - bobger - Nov-30-2017

can i use a if statement and in operator to trigger the code
corb = random.randrange(3)
print('talks about it with ' + talks[corb] + ' about his battle' + '.')
full code is
import random

fight =("man fights" , "man kills" , "man wounds" , "man cuts off head of" , "man slices off arm of")
killed =("mortally wounds it" , "stabs it in the eye" , "gets his arm cut off with it's sharp spikes" , "survives" , "gets his head cut of by it's sharp claws")
feelin = 'mortally wounds it'
bleeding = 'sharp spikes'
talks =("wife,children and friends over a few drinks at the local pub" , "his girlfriend and friends over a few drinks" , " other warriors over a few drinks at the local pub at the local pub")

fighting = random.randrange(4)


print (fight[fighting] + ' monster and ' + killed[fighting] + '.')
x =(fight[fighting] + ' monster and ' + killed[fighting] + '.')

if feelin in x:
   print("man goes home triumphant.Tells his friends about his great battle.")

corb = random.randrange(3)
print('talks about it with ' + talks[corb] + ' about his battle' + '.')

if bleeding in x:
   print("man almost bleeds to death but two other warriors come to help him out.one fights the monster.While the other one carries him away to safety.then puts him on his horse.then makes horse go fastly back to town")
  
   print("man is hurt bad")



RE: if statement and in operator problem - metulburr - Nov-30-2017

if feelin in x:
   print("man goes home triumphant.Tells his friends about his great battle.")
Yes that works.

Quote:print (fight[fighting] + ' monster and ' + killed[fighting] + '.')
I would get rid of concatenation now, especially if you are using a lot of text
read this tutorial
https://python-forum.io/Thread-Basic-string-format-and-string-expressions


RE: if statement and in operator problem - buran - Nov-30-2017

why not use random.choice(seq) instead of random.randrange(n)?
Also as it is now, you will always end with same pairs fight-killed, because you always use same index fighting to get elements from both lists


RE: if statement and in operator problem - bobger - Nov-30-2017

this is the output of my python program sometimes.

man wounds monster and gets his arm cut off with it's sharp spikes.
talks about it with his girlfriend and friends over a few drinks about his battle.
man almost bleeds to death but two other warriors come to help him out.one fights the monster.While the other one carries him away to safety.then puts him on his horse.then makes horse go fastly back to town
man is hurt bad.
if bleeding in x:
python snippet is only supposed to show the message,"man almost bleeds to death but two other warriors come to help him out.one fights the monster.While the other one carries him away to safety.then puts him on his horse.then makes horse go fastly back to town. man is hurt bad."

i will try it.


RE: if statement and in operator problem - bobger - Nov-30-2017

I forgot to mention I am using python 3.6.3.


RE: if statement and in operator problem - bobger - Nov-30-2017

random.choice
worked for my random story generator.