vendredi 19 octobre 2018

random return not matching outcome requirements

python3.7

I am trying to use a simple "dice" code to (originally) allow for a 50/50 chance between two outcomes. However I have noticed that this code does not consistently match the outcome I expect with the number rolled. For example, I can roll a 1 and get "This should be 3 or less" and then roll a 1 again directly after and get "This should be 4 or more". Can anyone see what is causing this?

import random

def dice():
    roll = random.randint(1,6)
    return roll

def count():
    print(dice())
    if dice() <= 3:
        print("This should be 3 or less")
    else:
        print("This should be 4 or more")

count()

edit: I realized that I may be calling dice() separately and tried this, which worked.

import random

def dice():
    roll = random.randint(1,6)
    return roll

def count():
    x = dice()
    print(x)
    if x <= 3:
        print("This should be 3 or less")
    else:
        print("This should be 4 or more")

count()




Aucun commentaire:

Enregistrer un commentaire