Python 3 - How to randomize what text displays
<p>I am a beginner at python and I am trying to figure out how I can randomly display text. For example, 60% chance of "Hello", 40% chance of saying "Goodbye", etc. Right now, for fun, I am trying to create sort of a bottle flip game. If you don't know what it is, its basically when you flip a half empty water bottle and try to land it. This is what I have: (This is more than likely completely wrong.)</p>

<pre><code>import random

number = random.randrange(10)

if number == "1":
    print ("You have landed the bottle!")
elif number == "2":
    print ("You have landed the bottle!")
elif number == "3":
    print ("You have landed the bottle!")
elif number == "4":
    print ("You have landed the bottle!")
elif number == "5":
    print ("The bottle did not land, better luck next time.")
elif number == "6":
    print ("The bottle did not land, better luck next time.")
elif number == "7":
    print ("The bottle did not land, better luck next time.")
elif number == "8":
    print ("The bottle did not land, better luck next time.")
elif number == "9":
    print ("The bottle did not land, better luck next time.")
elif number == "10":
    print ("The bottle landed on the cap!")
</code></pre>

<p>You have the right idea, basically, but you can greatly simplify your code.</p>

<pre><code>if number &lt; 5:
    print ("You have landed the bottle!")
elif number &lt; 10:
    print ("The bottle did not land, better luck next time.")
else:
    print ("The bottle landed on the cap!")
</code></pre>

<p>You can change the values in your call to <code>randrange</code> and in the if statements above to get just about any weighting you'd like.</p>

<p>Note that in your original question you were comparing numbers to strings. I changed that here. Comparing numbers to strings (i.e. <code>3 == "3"</code>) will always be <code>False</code>.</p>

python python-3.x random