Python Forum
if statement and in operator problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if statement and in operator problem
#1
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")
Reply
#2
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-str...xpressions
Recommended Tutorials:
Reply
#3
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
Reply
#4
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.
Reply
#5
I forgot to mention I am using python 3.6.3.
Reply
#6
random.choice
worked for my random story generator.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with while statement. BobSmoss 3 1,671 Jan-08-2022, 03:22 PM
Last Post: BobSmoss
  Problem in if-else statement shantanu97 2 2,441 Apr-09-2021, 06:37 AM
Last Post: shantanu97
  multiple condition if statement problem FelixReiter 3 2,609 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Problem with If statement and dataframe Milfredo 1 1,777 Sep-16-2020, 05:50 AM
Last Post: Milfredo
  Problem with If else statement Milfredo 5 2,597 Aug-30-2020, 06:32 PM
Last Post: Milfredo
  Problem with a 'break' statement. christopher3786 3 2,451 Jun-20-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Problem with an IF statement Ryan_Todd 13 5,025 Jan-30-2020, 08:22 PM
Last Post: snippsat
  Problem with 'and' in 'if' statement CoderMan 3 2,538 Oct-06-2019, 07:32 PM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,220 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Problem with elif statement Haddal99 2 2,283 May-20-2019, 09:26 AM
Last Post: avorane

Forum Jump:

User Panel Messages

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