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?

Postby ignacio_cabero » Sun Jun 15, 2014 4:30 am

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

Postby Yoriz » Sun Jun 15, 2014 9:05 am

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
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

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

Postby Kebap » Sun Jun 15, 2014 1:17 pm

Hi Ignacio, welcome to python! :D

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: 394
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

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

Postby ignacio_cabero » Sun Jun 15, 2014 3:23 pm

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'
If anyone knows how to fix this, please help.
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?

Postby ignacio_cabero » Sun Jun 15, 2014 3:25 pm

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?

Postby stranac » Sun Jun 15, 2014 3:55 pm

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.

Also, read the new users thread and start using code tags.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1135
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 6 guests