0

I'm trying to generate random sentences but it does not work, I already imported random and this is what I have:

    if "razor blade" in inventory:
        sentences = ("What do you take yourself for?","Are you insane?", "You are not doing that.", "You have to be joking.", "This isn't going to work.")
        num= random.randrange (0,5)
        print (sentences[num])
    elif "razor blade" not in inventory:
        print ("you don't own this item in your inventory.") 
4
  • 3
    What error do you get? Commented Oct 9, 2017 at 14:25
  • you can use else: instead of elif in this case, your current code should work though Commented Oct 9, 2017 at 14:44
  • I don't get an error message, it just repeats "What do you take yourself for?" every time Commented Oct 9, 2017 at 15:51
  • It sounds like you might be repeatedly re-seeding random to the same seed elsewhere in your program. Commented Oct 9, 2017 at 16:30

1 Answer 1

2

You can use random.choice:

import random
if "razor blade" in inventory:
    sentences = ("What do you take yourself for?","Are you insane?", "You are not doing that.", "You have to be joking.", "This isn't going to work.")
    print(random.choice(sentences))
elif "razor blade" not in inventory:
    print ("you don't own this item in your inventory.") 
Sign up to request clarification or add additional context in comments.

9 Comments

ah it didn't work :( it's just doing the same thing and repeating "What do you take yourself for?"
@John it works fine for me when I run it. Try again.
it still won't work. The code before it is: elif decision== "use razor on comb" or "use razor on bottle" or "use razor on sink" or "use razor on toilet" or "use razor on jail bars" or "use razor on locking mechanism":
maybe the "or" function creates the glitch?
@John that code in and of itself is incorrect. You need: elif decision == "use razor on comb" or decision == "use razor on bottle" or decision == ... etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.