Its a beginner program that involves playing (rock, paper, scissors, lizard, spock) Is my code correct? There aren't any syntax errors on IDLE but I cant seem to get it to work. Any ideas. I'm new to Python. Thanks very much

 def pnumber_to_name(player_number):
        # fill in your code below
        if player_number == 0:
            return rock
        elif player_number == 1:
            return Spock
        elif player_number == 2:
            return paper
        elif player_number == 3:
            return lizard
        else:
            return scissors



    # convert number to a name using if/elif/else
    # don't forget to return the result!

def cnumber_to_name(comp_number):
    # fill in your code below
    if comp_number == 0:
        return rock
    elif comp_number == 1:
        return Spock
    elif comp_number == 2:
        return paper
    elif comp_number == 3:
        return lizard
    else:
        return scissors


#def name_to_number(name):
    # fill in your code below

 #   if rock:
  #      name = 0
   # elif Spock:
    #    name = 1
#    elif paper:
 #       name = 2

  #  return name

# convert name to number using if/elif/else
# don't forget to return the result!


def rpsls(name):
    pnumber_to_name(player_number)
    cnumber_to_name(comp_number)
    player_number = random.randrange(0,5)
    comp_number = random.randrange(0,5)
    name = (player_number - comp_number)%5
    if name == 1 or 2:
        print ('Player chooses', pnumber_to_name(player_number))
        print ('Computer chooses', cnumber_to_name(comp_number))
        print ('Player wins')


    elif name == 3 or 4:
        print ('Player chooses', number_to_name(number,(player_number)))
        print ('Computer chooses', number_to_name(number,(comp_number)))
        print ('Comp wins')

    elif name == 0:
        print ('Player chooses', number_to_name(number,(player_number)))
        print ('Computer chooses', number_to_name(number,(comp_number)))
        print ('Player and computer tie')
share|improve this question
Where are rock, Spock etc defined? What errors do you get exactly? – Mischa Arefiev Oct 19 '12 at 12:19
You shouldn't try to build switch logic in Python that way, use dicts instead. – Rik Poggi Oct 19 '12 at 12:46

closed as off topic by palacsint, Brian Reichle, Trevor Pilley, Glenn Rogers, Jeff Vanzella Oct 19 '12 at 16:21

Questions on Code Review - Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

All you did was define a number of functions. Nowhere in this code is there anything to call these functions. You should have something like this at the bottom of you code to have it do something when you run it.

if __name__ == '__main__':
  #call the functions you defined

Unless there is other code that you didn't post, there are many errors in your code.

  • rock, Spock, paper, lizard and scissors are never defined anywhere. Do you want to return strings here?

  • rpsls() uses player_number and comp_number before they are defined.

  • if name == 1 or 2: is not going to do what you want. This is equivalent to if (name == 1) or 2:. And since 2 will always evaluate to true, you will always enter that if block.

  • cnumber_to_name() and pnumber_to_name() have the same implementation, just different variable names. Remove one of them and rename the other to_name(). You call number_to_name() but it is never defined. From the name, it sounds like it does what my to_name() function would do.

  • rpsls() is a terrible name for a function. Quickly looking at it, it looks like you just mashed the keyboard. After thinking about it, i figured out what it was, but you should make people have to do that.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.