## How to check for 50% or higher chance?

This is the place for queries that don't fit in any of the other categories.

### How to check for 50% or higher chance?

Hi, so i'm making a simple RPG style text-based game, and I want some creatures to have "hidden" abilities. For example, I want there to be a 50% chance of getting 2+ all stats. I figured out how to use the 'import random random.random()' command, and i want to say 'If number is higher than 0.5, then it gains 2+ all stats. is there a way of assigning the 'random.random()' to a variable and saying 'If 'variable' is == to 0.5 or higher: print("Blah Blah has 2+ all stats.")

Sorry if this sounds confusing, I'm sure there's someone who will understand.
Thank you all.
Ignacio Cabero
Last edited by Yoriz on Sun Jun 15, 2014 9:07 am, edited 2 times in total.
Reason: First post lock, Changed tittle.
ignacio_cabero

Posts: 6
Joined: Sun Jun 15, 2014 4:25 am

### Re: Game Help

In the following link it shows the comparisions available in python, see greater then or equal too.
https://docs.python.org/3/library/stdtypes.html#comparisons
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: How to check for 50% or higher chance?

Hi Ignacio, welcome to python!

ignacio_cabero wrote:is there a way of assigning the 'random.random()' to a variable and saying 'If 'variable' is == to 0.5 or higher: print("Blah Blah has 2+ all stats.")

Yes, this is definitely possible, and also very easy. Please give it a try and then report back here to show us your code.

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap

Posts: 689
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: How to check for 50% or higher chance?

Ok so, In theory,
'Y=0.5
import random
X=random.random()
if 'X'>= Y: print("Blah Blah has +2 stats")
if 'X'< Y: print("Blah Blah is about to attack!")
print(X)'
This should work, but every time I hit run, it says "Blah Blah has +2 stats" even if 'X' is lower than 'Y'
Thank You,
Ignacio Cabero
ignacio_cabero

Posts: 6
Joined: Sun Jun 15, 2014 4:25 am

### Re: How to check for 50% or higher chance?

Ok so I fixed the problem! I forgot to add apostrophes around the Y, indicating that its a variable.
EDIT: Actually, now, it only says "Blah Blah is about to attack!" even if the number is greater than 'Y'
ignacio_cabero

Posts: 6
Joined: Sun Jun 15, 2014 4:25 am

### Re: How to check for 50% or higher chance?

Quotes create strings, not variables. You're comparing the string 'X' with the string 'Y'. 'X' is always lower, since it comes first in alphabet.
Remove the quotes in your comparisons and you should be fine.

Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm