Rock Paper Scissors Lizard Spock

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Rock Paper Scissors Lizard Spock

Postby XndrK » Thu Oct 17, 2013 10:43 pm

If anyone knows of a way I could clean it up, let me know. In the meantime, enjoy!

Code: Select all
# A note: This was created as part of a Coursera course in Python called "Introduction to Interactive Programming in Python".

# helper functions

def number_to_name(number):
    """Converts computer number to name of weapon"""
    name = None
    name_list = ["rock", "Spock", "paper", "lizard", "scissors"]
    if number in range(5):
        name = name_list[number]
    else:
        print "number_to_name problem!"
    return name

   
def name_to_number(name):
    """Converts name of weapon to computer number"""
    number = None
    name_list = ["rock", "Spock", "paper", "lizard", "scissors"]
    if name in name_list:
        number = name_list.index(name)
    else:
        number = None
    return number


def rpsls(name):
    """Gameplay of Rock Paper Scissors Lizard Spock"""
   
    #import random
    #decomment above once done testing
   
    # convert name to player_number using name_to_number
    player_number = name_to_number(name)
   
    # compute random guess for comp_number using random.randrange()
    #comp_number = random.randrange(0, 5)
    comp_number = 4 #test
    # compute difference of player_number and comp_number modulo five
    try:
        verdict = (player_number - comp_number) % 5
    except TypeError:
        verdict = None
    # use if/elif/else to determine winner
    who_wins = None
    if verdict == 1 or verdict == 2:
        who_wins = "Player"
    elif verdict == 3 or verdict == 4:
        who_wins = "Computer"
    elif isinstance(verdict, int): #I think this is the problem
        who_wins = "Tie"
    else:
        who_wins = "error"

    # convert comp_number to name using number_to_name
    comp_choice = number_to_name(comp_number)
    # print results
    print ""
    print "Player chooses", name
    print "Computer chooses", comp_choice
    if who_wins == "Tie":
        print "Nobody wins. That was anticlimactic."
    elif who_wins != "error":
        print who_wins, "wins!"
    else:
        print "Player DQ'd for banned weapon."
   
play = True

def want_to_play():
    want_to_play = raw_input("Do you want to play again? y/n ")
    if want_to_play[0].lower() == "y":
        return True
    else:
        return False

while play:
    player_choice = raw_input("Enter rock, paper, scissors, lizard, or Spock (with capitalization). ")
    rpsls(player_choice)
    play = want_to_play()
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 131
Joined: Sat Jun 15, 2013 7:57 pm

Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests