0

I'm trying to declare a few simple variables as part of a function in a very basic collision detection programme. For some reason it's rejecting my variables (although only some of them even though they're near identical). Here's the code for the function;

def TimeCheck():
    timechecknumber = int(time.time())
    timecheckdiv = backcolourcheck % 5
    if timecheckdiv < 1:
        timecheck = true
    else:
        timecheck = false
    if timecheck == true:
        backgroundr = (int(random.random()*255)+1
        backgroundg = (int(random.random()*255)+1
        backgroundb = (int(random.random()*255)+1

for some reason it accepts backgroundr but not backgroundg, anyone got any ideas why? thanks

2
  • 1
    What does "accepts" mean? Do you have a specific error message or other problem? Or are you having trouble balancing your ()'s? Commented Feb 26, 2009 at 16:55
  • I meant that it was just highlighting backgroundg in red and saying syntax error. it was just a parenthesis issue though :) Commented Feb 26, 2009 at 16:59

2 Answers 2

8

You have mismatched parentheses on the line beginning with backgroundr. I think maybe you want this:

backgroundr = int(random.random() * 255) + 1

Note that each of the next two lines also have mismatched parentheses, so you'll have to fix those, too.

Sign up to request clarification or add additional context in comments.

2 Comments

You need to multiply by 255 before casting to int.
As noted below, if you want the code to do something useful (which you probably do), you most likely actually want the parenthesis in the order below (I've edited the answer to reflect this).
2

mipadi's answer will always yield a 1. You need to multiply by 255 before you cast to int. Try this.

backgroundr = int(random.random() * 255) + 1

1 Comment

You're assuming -- of course -- that this is what they want. Since the ()'s were incorrect, perhaps they really did want an answer of 1 each time ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.