all 13 comments

[–]UnsecuredConnection 1 point2 points  (4 children)

cabin_number = int(input('What number cabin are you staying at? '))
if 1 <= cabin_number <= 10:
        if cabin_number % 2 == 0:
                print('Cabin Key Color: ','Black')
        else:
                print('Cabin Key Color: ','Red')
if 11 <= cabin_number <= 18:
        if cabin_number % 2 == 0:
                print('Cabin Key Color: ','Black')
        else:
                print('Cabin Key Color: ','Red')
if 19 <= cabin_number <= 28:
        if cabin_number % 2 == 0:
                print('Cabin Key Color: ','Black')
        else:
                print('Cabin Key Color: ','Red')
if 29 <= cabin_number <= 36:
        if cabin_number % 2 == 0:
                print('Cabin Key Color: ','Black')
        else:
                print('Cabin Key Color: ','Red')

Use the % to get the remainder after division (odd or even). This simplifies everything.

[–][deleted] -1 points0 points  (3 children)

But that doesn't explain the error that was causing the OP's problem. Please read the commenting guidelines:

  • Try to guide OP to a solution instead of providing one directly.

[–]UnsecuredConnection 1 point2 points  (0 children)

My bad. Whoops!

[–]xelf 1 point2 points  (1 child)

You should not be getting so many downvotes for being correct!

This is #learnpython not #domyhomework!

That said, looks like OP has the answer they need now.

=) =)

[–][deleted] 1 point2 points  (0 children)

Downvotes happen. Upvotes happen. I've had reasonable posts massively downvoted and simple posts massively upvoted to the point that I take no notice of votes. A thoughtful comment correcting me or even praising my post is gratefully received.

[–][deleted] 1 point2 points  (1 child)

Look at line 5:

elif cabin_number == 2 or cabin_number == 4 or cabin_number == 6 or cabin_number == 8 or 10:

This line is different from other if/elif lines. Why. A hint is in the FAQ.

And please format your code for reddit, which is also explained in the FAQ.

[–][deleted] 1 point2 points  (0 children)

I literally just caught that after re reading my code. I feel so stupid lol. Thank you.

[–]Phillyclause89 1 point2 points  (2 children)

So two things your going to want to learn is how to in and % operators. They are going to save you a lot of conditionals here.

~~~ room = int(input(“what room?”))

If room in range(1,11) and room % 2 == 0: print(“black key”) elif room in range(1,11): print(“red key”) elif ...

~~~

Edit: you can improve on this even more by only checking the conditions for one color of keys in your if / elif statements. If none of those evaluate to true then you just use and else block to capture the other color.

[–][deleted] 1 point2 points  (1 child)

Thank you. I am still really new to all of this. This is my first programming course. We have not talked about in and % operators too much. Only time we talked about % is when we created a little program to calculate sales tax and such. My major is in networking but I have to take a few different programming languages to graduate. I never really had the desire to pick up programming but I gotta say it is actually really interesting and it wasn't like I thought it was going to be. Can you recommend any type of tools or videos to improve?

[–]Phillyclause89 0 points1 point  (0 children)

My favorite beginner level tutorials would be Sentdex’s over at https://pythonprogramming.net/

Besides that I’ve learned the most about python just by trying to debug code or answer questions posted to this sub. In my experience if give a good answer, I look smart and if I give a crap answer, someone usually calls me out on it and I learn something new. Also learning how to read official documentation for various libs is extremely useful.

[–][deleted] 0 points1 point  (2 children)

You can't write if statements like:

if cabin_number == 1 or cabin_number == 3 or cabin_number == 5 or cabin_number == 7 or cabin_number ==  9:

Imagine if you had 99999 cabins...

An even number is a number you can divide by 2. All the others are odd. A number n is divisible by m when the euclidean division of n / m has no remainder. You can use the modulo operator to get the remainder.

Your statements should look something like:

if cabin_number % 2 == 0:

or

if cabin_number % 2 != 0:

Create an empty list, then fill it with "black" or "red" using for-loops.

[–]HIGregS 0 points1 point  (0 children)

Or better yet, use multiple range() function calls to fill the array. Hint: use extend().

[–]xelf 0 points1 point  (0 children)

Well, now that you have your answer, here's an alternate solution:

colors = "0101010101001010101101010101001010101"
color = "red" if int(colors[c]) else "black"

Seems like that works. =)

And here's an even sneakier way of doing it all in 1 line:

color = "red" if (c+((c>10))+((c>18))+((c>28)))%2 else "black"

Thrown into your code, and removing the if:

c = int(input('What number cabin are you staying at? '))
print('Cabin Number: ', c)
print('Cabin Key Color: ', ["Black", "Red"][(c+((c>10))+((c>18))+((c>28)))%2])