Jun-05-2017, 12:24 PM
(This post was last modified: Jun-05-2017, 12:24 PM by SadoDeomeoon.)
For a little art prompt generator I'm making, I needed the program to generate random integers corresponding to colours, themes and subjects. For colours, I want it to pick either 1, 2 or 3 colours. For 3 colours I need it to generate three random integers (corresponding to colours in a list) and check them so they're not the same. I used a function to do so:
I'm a bit new to this programming lark: is there something I'm massively overlooking? Or, do you have an alternative method that's better?
Thanks.
PS. I feel like I should mention that the function checking only two colours works perfectly, as does everything else. The problem may lie elsewhere, though, so here's the rest of the hideously newb-ish code:
def fColours3(): RandColours1 = randint(0,len(Colours2)); RandColours2 = randint(0,len(Colours2)); while RandColours1 == RandColours2: RandColours2 = randint(0,len(Colours2)); RandColours3 = randint(0,len(Colours2)); while RandColours1 == RandColours3 or RandColours3 == RandColours2: RandColours3 = randint(0,len(Colours2)); print (Colours2[RandColours1-1]+",",Colours2[RandColours2-1]+",",Colours2[RandColours3-1]); return;Sometimes it will output something along the lines of "Pink, Pink, Blue" or "Yellow, Red, Yellow" so I can only assume it's not comparing them properly.
I'm a bit new to this programming lark: is there something I'm massively overlooking? Or, do you have an alternative method that's better?
Thanks.
PS. I feel like I should mention that the function checking only two colours works perfectly, as does everything else. The problem may lie elsewhere, though, so here's the rest of the hideously newb-ish code:
from random import randint def fColours2(): RandColours1 = randint(0,len(Colours2)); RandColours2 = randint(0,len(Colours2)); while RandColours1 == RandColours2: RandColours2 = randint(0,len(Colours2)); print (Colours2[RandColours1-1]+",",Colours2[RandColours2-1]); return; def fColours3(): RandColours1 = randint(0,len(Colours2)); RandColours2 = randint(0,len(Colours2)); while RandColours1 == RandColours2: RandColours2 = randint(0,len(Colours2)); RandColours3 = randint(0,len(Colours2)); while RandColours1 == RandColours3 or RandColours3 == RandColours2: RandColours3 = randint(0,len(Colours2)); print (Colours2[RandColours1-1]+",",Colours2[RandColours2-1]+",",Colours2[RandColours3-1]); return; Theme = ["Anger","Beauty","Bravery","Celebration","Discovery","Dreams","Exhilaration","Fear","Freedom","Friendship","Heartbreak","Imprisonment","Inhibition","Love","Memory","Motion","Mourning","Novelty","Panic","Regal","Sickness","Smug","Solitude","Sorrow","Stubbornness","Time","Travel","Violence","Visit","Wild","Work"] Subject = ["Alien","Amphibian","Arthropodian","Avian","Burrowing Creature","Chimera","Dead/Undead","Draconic","Eldritch Abomination (disregard for the laws of the universe)","Air Elemental","Earth Elemental","Fire Elemental","Water Elemental","Fey","Flying Creature","Food Creature","Holy Being","Humanoid","Inanimate Object Creature","Mammal","Mechanical Being","Mythical Being","Piscean (Fishy)","Plant Creature","Prehistoric (non-dinosaur)","Reptilian","Saurian (Dinosaur-like)","Spirit","Supernatural Being (not un-dead or spirit)","Aquatic Creature (non-fish)","Unholy Being"] Colours = ["Tri-Tone","Two-Tone","Black","White","Grey","Sepia","Red","Yellow","Blue","Green","Orange","Purple","Pink"] Colours2 = ["Black","White","Grey","Sepia","Red","Yellow","Blue","Green","Orange","Purple","Pink"] Roll = input("Roll? ") while Roll != "no" and Roll != "n": print(Theme[randint(1,len(Theme))-1]); print(Subject[randint(1,len(Subject))-1]); #RandColours = randint(1,len(Colours)); #RandColours = RandColours - 1; RandColours = 0; if RandColours >= 2: print(Colours[RandColours]); elif RandColours == 1: fColours2(); else: fColours3(); Roll = input("Roll? "); print ("Goodbye!");