[Tutor] Newbie problems
Alan Gauld
alan.gauld at btinternet.com
Thu Apr 30 10:40:14 CEST 2015
On 30/04/15 04:58, 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
The first problem is that you are posting in HTML which is losing
all the code formatting, making it hard to read.
Also you don't actually tell us what doesn't work. If you get an
error message post it. If you get different results to what you
expect tell us what you expect and what you got.
For now I'll take a guess below...
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')
Note that you are testing the number for equality, which means it
must be exactly the same value.
But your red_number and black_number are both tuples of several
numbers so a single number will never equal a sequence of numbers.
You should use the 'in' test instead of equals.
Like
if number in red_number:
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list