Instead of choosing the colours for your flag before you start, you can get the computer to choose for you!
There are two ways to do this:
At the very start of your program, add this line:
from random import choice
This imports a library, and tells Python that you want to use these new bits of code.
Next, you need to choose your colours, and put them into your program in a list, which is a special type of Python data.
my_colours = ["green", "pink", "blue", "orange"]
This puts all of your chosen colours together, and calls the group my_colours
.
Decide what part of your program you want to use the random colours in - for example, do you want your first rectangle to be a random colour?
Find the bit of your code that chooses a colour, it will look somethinglike this at the moment:
color('green', 'yellow')
Change this line, so that it now looks like this:
color(choice(my_colours), choice(my_colours))
The computer will now choose a random outline, and a random fill colour, from the list you made!
At the very start of your program, add this line:
from random import randint
This imports a library, and tells Python that you want to use these new bits of code.
If you want to use any colour that exists, you can't use the colour names any more. Instead, we choose the amount of Red, Green, and Blue make up the colour.
To use this new system in your program, you need to change the colormode
, to tell it you are using RGB instead of names. If you want to use both types of colours in your program, you will need to change the colour mode every time.
Decide what part of your program you want to use the random colours in - for example, do you want your first rectangle to be a random colour?
Find the bit of your code that chooses a colour, it will look somethinglike this at the moment:
color('green', 'yellow')
Replace this line this the below code:
colormode(255) color(randint(0,255), randint(0,255), randint(0,255)) colormode(1)
This changes the colormode, sets a random amount of red green and blue for your colour, then changes the colormode back (so you can keep using names in other parts of your program).