[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Quick question from newbie



Yes you are. "if i == range(1,9)" compares i against the iterator returned by the range function. Assuming i is a number, it will never be equal (since a number is not an iterator). Your function would print "It worked!" if you called print_i(range(1,9)).

I think what you probably want is "if 1 <= i < 9" or something similar. You could equivalently write "if i in range(1,9)" but the other way is preferred because using in like this actually loops over every number between 1 and 9 and checks whether that number is equal to i.

Hope this helps,

Mike

Jonah Fishel wrote:
If I had this:

def print_i(i):
    if i == range(1, 9):
        print "It worked!"
    else:
        print "Bummer!"

print_i(6)

Why would I get Bummer! every time? Am I misusing range()?

Thanks,
globalp