[Tutor] Newbie problems

Dave Angel davea at davea.name
Thu Apr 30 10:27:30 CEST 2015


On 04/29/2015 11:58 PM, Jag Sherrington wrote:
> Can anyone please tell me what I am doing wrong?As this code I have for the Roulette Wheel colours exercise, won't work. number = int(input('Enter a number between 0 and 36: '))green_number = (0) red_number = (1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36) black_number = (2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 34, 29, 31, 33, 35)
> if number == green_number:    print('Number is Green')    elif number == red_number:    print('Number is Red')    elif number == black_number:        print('Number is Black')
> Thank you for any help, Jag
>

Please post your code the way you're going to run it, like one statement 
per line, indented as required, etc.

Your problem is you're comparing an int (number) to a list (green_number 
or red_number or black_number).  They'll never be equal so it won't 
print anything.

presumably you don't want to know if the number is equal to the list, 
but whether it's in the list.  The "in" operator will tell you that.

Something like:
     if number in green_numbers:

(I changed it to plural, so it'd be more obvious that it's not a single 
value, but a list of them.  Little things like that can make errors much 
easier to spot.)


-- 
DaveA


More information about the Tutor mailing list