Python 3 random.randint() only returning 1's and 2's

266 views Asked by At

I thought I'd finally worked out all the kinks in the dice rolling code I wrote for practice, but then apparently I forgot to save my work last night, and when I rewrote it this afternoon, the random.randint function didn't seem to be returning any numbers other than 1's and 2's, where before I remember it returning much higher numbers when given larger sizes of dice. I ran the rolling function by itself in the shell and it seemed to be working fine, but I think there must be something wrong with my overall code that's causing it to treat every roll as a 1d2.

import random

die = 0
num = 0

def Opening():
    print ("Welcome to the Super Deluxe Diceroller mk. 3!")
    print ("What sort of Dice would you like to roll")
    choice()

def choice():
    print ("A. d2")
    print ("B. d4")
    print ("C. d6")
    print ("D. d8")
    print ("E. d10")
    print ("F. d12")
    print ("G. d20")
    print ("H. d100")
    global sides
    die = input()
    if die == 'a' or 'A':
        sides = 2
        nombre()
    elif die == 'b' or 'B':
        sides = 4
        nombre()
    elif die == 'c' or 'C':
        sides = 6
        nombre()
    elif die == 'd' or 'D':
        sides = 8
        nombre()
    elif die == 'e' or 'E':
        sides = 10
        nombre()
    elif die == 'f' or 'F':
        sides = 12
        nombre()
    elif die == 'g' or 'G':
        sides = 20
        nombre()
    elif die == 'h' or 'H':
        sides = 100
        nombre()
    else:
        return 'Invalid'
        choice()

def nombre():
    print ("How many dice would you like to roll?")
    global num
    num = input()
    if num.isnumeric():
        roll()
    else:
        return 'Invalid'
        nombre()

def roll():
   global num
    global fortuna
    global sides
    if int(num) > 0:
        fortuna = random.randint(1,sides)
        num = int(num)
        num = num - 1
        print(fortuna)
        roll()
    else:
        retry()

def retry():
    print("Would you like to roll again? (y/n)")
    ans = input()
    if ans == 'y':
        Opening()
    elif ans == 'n':
        exit
    else:
        return 'Invalid'
        retry()       

Opening()
1

There are 1 answers

0
krethika On BEST ANSWER

this line if die == 'a' or 'A': will always return True. That is because the way the items are grouped, it is the same as if (die == 'a') or ('A'): and 'A' (along with any string except "") is always True.

Try changing all those lines to if die == 'a' or die == 'A": and it should solve your problem. Also see Truth Value Testing for more info about why this is True.