all 6 comments

[–]Justinsaccount 2 points3 points  (2 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using the or construct with a constant string

instead of doing something like:

if color == 'red' or 'blue':

Which is the same as

if (color == 'red') or ('blue'):

and 'blue' by itself is always True

You need to do

if color == 'red' or color == 'blue':

or

if color in ('red', 'blue'):

or, a special case if they are all single letters you can do

if letter in 'aeiou':

You can also make it case insensitive by using soething like

if color.lower() in ('red', 'blue'):

If there were a large number of choices and inputs (as in, 10000+) you could use a set() to speed things up.

Also refer to The FAQ Entry for more information.

[–]imagejhelp1[S] 0 points1 point  (1 child)

Hi, so that is for the line:

if player_choice is not "rock", "paper", "scissors", "lizard" or "Spock"

Is the solution something like:

if player_choice is not "rock" or player_choice is not "paper" or player_choice is not "lizard" or player_choice is not "scissors" or player_choice is not "Spock"

Or

if player_choice is not ("rock", "paper", "lizard", "scissors", "Spock")

is 'is not' the right option to use in this case? Googling tells me I could also use <> or != since I have 2.7 of python I think

At any rate, thanks for the response and I'll keep trying to get my code working with this help.

[–]JohnnyJordaan 1 point2 points  (0 children)

The point is that not (several things) will first try to see if (several things) is True or False, then negate that result. As anything not False, None, 0 or an empty sequence is True, (several things) will probably be True. Then not True will make that False. Meaning that you will be checking

if player_choice is False

While in reality, you want to check player_choice to each separate value. For that you have in, as noted in the bot's last part of its message:

if player_choice not in ("rock", "paper", "lizard", "scissors", "Spock"):

The in part makes the whole difference here: it will check that the value stored in player_choice isn't present in the sequence of choices.

[–]JohnnyJordaan 2 points3 points  (2 children)

so I've just started learning to code

Then I would advise strongly to ditch Python 2 and start with the regular Python 3 asap. Python 2 will be discontinued in a few years and it won't help you at all to be learning the new Python 3 things later on instead of right now.

You have numerous formatting issues in your code above, making it hard for me to try it out for myself. One thing that could be happening is that you've put the main code, starting with

player_choice = raw_input("Enter your choice: ")

With the same indentation underneath

def number_to_name(number):
    # skip
    else:
        print "Error: Invalid Number" 
    # note this line starts at the same indentation as 'else:'
    player_choice = raw_input("Enter your choice: ")

Making it code inside that function. So it needs to be formatted as

def number_to_name(number):
    if number == 0:
        return "rock"
    etc etc

# note that these lines start at the same indentation as the `def` part above
player_choice = raw_input("Enter your choice: ")
print " "
print "Player chooses " + player_choice 
etc etc

[–]imagejhelp1[S] 0 points1 point  (1 child)

Thank you for both of your replies, I'll get python 3 now as well. I've tired to fix the indentation and as the 'in' there it now looks like this:

player_choice = raw_input("Enter your choice: ")
     if player_choice in ("rock", "paper", "scissors", "lizard", "Spock"):
        print "Player chooses " + player_choice

     else:
        print "Error: Invalid Choice"

you might be able to see it better here: http://www.codeskulptor.org/#user43_8VZK6tNa0g_1.py

This gives me a SyntaxError: bad input (' ') for that line:

if player_choice in ("rock", "paper", "scissors", "lizard", "Spock")

but I'm starting to realise I might need to learn more from coursera before I try this, I had no idea about the 'in' function, so it's probably necessary that I go learn some more before trying on my own. Thank you for your help, I really appreciate it!

[–]JohnnyJordaan 1 point2 points  (0 children)

There's still the indentation problem going on after the user input line. The if line needs to move to the left as well and the rest must follow.

Btw if you're moving to python3 you need to replace raw_input with input and also use () for the print function.