Reed Merrill
Reed Merrill

Reputation: 128

How to store integers sepated by commas in a variable in Python

I am trying to store randomly generated integers in a single variable. They need to be separated by commas such that I can put the variable into a function and it will accept the syntax.

from SimpleGraphics import *

import random

pieColourR = random.randint(0, 255)
pieColourG = random.randint(0, 255)
pieColourB = random.randint(0, 255)

colourA = (pieColourR, pieColourG, pieColourB)

setFill(colourA)
rect(200, 100, 400, 400)

The three RGB values need to be accepted by the "setFill" function. I am doing this to randomly generate 3 different colours that will be consistent for the entire run time of the program.

Right now I get this error:

_tkinter.TclError: unknown color name "166 134 15"

Upvotes: 1

Views: 107

Answers (2)

Larry Lustig
Larry Lustig

Reputation: 50970

ukemi has answered your question correctly. You do not want to pass three arguments into the setFill() function, you want to pass a single argument with a known name or, alternatively, a pseudo-name created using the method ukemi described.

However, there are cases in which you want to convert a tuple or list into separate arguments to pass into a function. This can be done with Python's unpacking syntax:

someFunction(*colorA)

Upvotes: 2

iacob
iacob

Reputation: 24181

You need to pass the argument to setFill in an expected format. An acceptable one is thus:

colourA = color_rgb(pieColourR, pieColourG, pieColourB)
setFill(colourA)

See here for more info:

http://anh.cs.luc.edu/handsonPythonTutorial/graphics.html#random-colors

Upvotes: 1

Related Questions