function - Python 3 random.randint() only returning 1's and 2's -
i thought i'd worked out kinks in dice rolling code wrote practice, apparently forgot save work last night, , when rewrote afternoon, random.randint function didn't seem returning numbers other 1's , 2's, before remember returning higher numbers when given larger sizes of dice. ran rolling function in shell , seemed working fine, think there must wrong overall code that's causing treat every roll 1d2.
import random die = 0 num = 0 def opening(): print ("welcome super deluxe diceroller mk. 3!") print ("what sort of dice 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 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 roll again? (y/n)") ans = input() if ans == 'y': opening() elif ans == 'n': exit else: return 'invalid' retry() opening()
this line if die == 'a' or 'a':
return true. because way items grouped, same if (die == 'a') or ('a'):
, 'a'
(along string except ""
) true.
try changing lines if die == 'a' or die == 'a":
, should solve problem. see truth value testing more info why true.
Comments
Post a Comment