Discussion:
[Tutor] Newbie problems
Jag Sherrington
2015-04-30 03:58:25 UTC
Permalink
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

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/
Dave Angel
2015-04-30 08:27:30 UTC
Permalink
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-04-30 08:40:14 UTC
Permalink
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-05-02 07:58:34 UTC
Permalink
Please use ReplyAll to include everyone on the the list.
Hi Alan
Thanks for your help. I followed your instruction and get the
Enter a number between 0 and 36: 9
File "C:\Python34\Tests\Roulette_wheel_colours.py", line 8, in <module>
TypeError: argument of type 'int' is not iterable
Think about what I said rather than applying it blindly.

I said the red and black variables were both tuples and so needed an
'in' test.
Can you see the logic of that. Why it needs to be 'in' rather than equality?

I made no mention of green because it is not a tuple.
Can you see why 'in' is not the right test for green. Can you see
why the interpreter is complaining?

Never change code randomly or just because somebody else says
so (or you think they said so). Always underdstand *why* you are doing it.
*This is what I programmed:*
number = int(input('Enter a number between 0 and 36: '))
green_numbers = (0)
red_numbers = (1, 3, 5, 7, 9, 12, 14, 16, 18, 19,\
21, 23, 25, 27, 30, 32, 34, 36)
black_numbers = (2, 4, 6, 8, 10, 11, 13, 15, 17, 20,\
22, 24, 26, 28, 34, 29, 31, 33, 35)
print('Number is Green')
print('Number is Red')
print('Number is Black')
--
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

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Sibylle Koczian
2015-05-02 18:30:20 UTC
Permalink
Post by Alan Gauld
I made no mention of green because it is not a tuple.
Can you see why 'in' is not the right test for green. Can you see
why the interpreter is complaining?
*This is what I programmed:*
number = int(input('Enter a number between 0 and 36: '))
green_numbers = (0)
Additional hint: It's the comma that makes a tuple, not the parentheses.
So (0) is no tuple, (0,) would be one.
Post by Alan Gauld
red_numbers = (1, 3, 5, 7, 9, 12, 14, 16, 18, 19,\
21, 23, 25, 27, 30, 32, 34, 36)
black_numbers = (2, 4, 6, 8, 10, 11, 13, 15, 17, 20,\
22, 24, 26, 28, 34, 29, 31, 33, 35)
print('Number is Green')
print('Number is Red')
print('Number is Black')
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Dave Angel
2015-05-02 10:13:13 UTC
Permalink
1) Please reply-list, or if your email program doesn't support that, do
a reply-all. The idea is to make sure ***@python.org is in your To:
field. Otherwise you're just leaving private messages, and that's not
what a public forum like this is about.

2) Please use text email, not html. As you can see below, your
formatting was thoroughly trashed by your email program. I took a
message at the html in your message, and you tried to use color as well,
which won't be visible by most people here.
Enter a number between 0 and 36: 9Traceback (most recent call last): File "C:\Python34\Tests\Roulette_wheel_colours.py", line 8, in <module> if number in green_numbers:TypeError: argument of type 'int' is not iterable
number = int(input('Enter a number between 0 and 36: '))green_numbers = (0) red_numbers = (1, 3, 5, 7, 9, 12, 14, 16, 18, 19,\ 21, 23, 25, 27, 30, 32, 34, 36) black_numbers = (2, 4, 6, 8, 10, 11, 13, 15, 17, 20,\ 22, 24, 26, 28, 34, 29, 31, 33, 35)
if number in green_numbers: print('Number is Green')
elif number in red_numbers: print('Number is Red')
elif number in black_numbers: print('Number is Black')
Kind regards, Jag BraveArt Multimedia
0421176576
Your remaining problem is that green_numbers isn't a list or tuple, it's
just a single number. You can change either of the following ways:

green_numbers = (0,) #The comma forces it to be a tuple
or
green_numbers = [0] #it's a list

Alternatively, you could use == for green, and in for the other two.
But as good programming practice, it's good to keep symmetry for the 3
cases.
--
DaveA

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...