Coursera-an Introduction to Interactive programming in Python (Part 1)-mini-project-rock-paper-scissors-lizard-spock

Source: Internet
Author: User
Tags diff

Mini-project Description-rock-paper-scissors-lizard-spock

Rock-paper-scissors is a hand game this is played by the people. The players count to three in unison and simultaneously "throw" one of the three hand signals this correspond to rock, paper O R Scissors. The winner is determined by the rules:

    • Rock smashes scissors
    • Scissors cuts paper
    • Paper covers Rock

Rock-paper-scissors is a surprisingly popular game this many people play seriously (see the Wikipedia article for details) . Due to the fact that a tie happens around 1/3 of the time, several variants of rock-paper-scissors exist that include more Choices to make ties less likely.

Rock-paper-scissors-lizard-spock (RPSLS) is a variant of rock-paper-scissors that allows five choices. Each choice wins against the other choices, loses against the other choices and ties against itself. Much of Rpsls ' s popularity is the It had been featured in 3 episodes of the TV series "The Big Bang theory". The Wikipedia entry for Rpsls gives, the complete description of the details of the game.

In our first mini-project, we'll build a Python function rpsls(name) that takes as input the string name , which is one of the ,,, "paper" "scissors" "lizard" , or "Spock" . The function then simulates playing a round of rock-paper-scissors-lizard-spock by generating it own random choice from T hese alternatives and then determining the winner using a simple rule that we'll next describe.

While Rock-paper-scissor-lizard-spock have a set of ten rules that logically determine who wins a round of RPSLS, coding up These rules would require a large number (5x5=25) of if / elif / else clauses in your mini-project code. A simpler method for determining the winner are to assign each of the five choices a number:

    • 0-rock
    • 1-spock
    • 2-paper
    • 3-lizard
    • 4-scissors

In this expanded list, each choice wins against the preceding the choices and loses against the following-choices (if Rock and scissors are thought of as being adjacent using modular arithmetic).

In any of the mini-projects for this class, we'll provide a walk through of the steps involved in building your project To aid its development. A template for your mini-project are availablehere. Please refer to this template.

http://www.codeskulptor.org/#user40_8h6EmKBtPG3oWfv_0. py

#rock-paper-scissors-lizard-spock Template#The key idea of this program is to equate the strings#"Rock", "paper", "Scissors", "lizard", "Spock" to numbers#As follows:##0-rock#1-spock#2-paper#3-lizard#4-scissors#helper FunctionsdefName_to_number (name):#Delete the following pass statement and fill in your code below    if(Name = ="Rock"):        return0elif(Name = ="Spock"):        return1elif(Name = ="Paper"):        return2elif(Name = ="Lizard"):        return3elif(Name = ="Scissors"):        return4Else:        return "Error"        #convert name to number using If/elif/else    #don ' t forget to return the result!defnumber_to_name (number):#Delete the following pass statement and fill in your code below    if(Number = =0):return "Rock"    elif(Number = = 1):        return "Spock"    elif(Number = = 2):        return "Paper"    elif(Number = = 3):        return "Lizard"    elif(Number = = 4):        return "Scissors"    Else:        return "Error"        #convert number to a name using If/elif/else    #don ' t forget to return the result!    defRpsls (player_choice):#Delete the following pass statement and fill in your code below            #print a blank line to separate consecutive games    Print ""    #Print out the message for the player ' s choice    Print "Player chooses", Player_choice#convert the player ' s choice to player_number using the function name_to_number ()Player_number =Name_to_number (Player_choice)#compute random guess for comp_number using Random.randrange ()    ImportRandom Comp_number= Random.randrange (0, 5)    #convert Comp_number to Comp_choice using the function number_to_name ()Comp_choice =number_to_name (Comp_number)#print out the message for computer ' s choice    Print "Computer chooses", Comp_choice#compute difference of comp_number and player_number modulo fivediff = comp_number-player_number% 5; #Use If/elif/else to determine winner, print winner message    if(diff = =-3ordiff = =-4ordiff = = 1ordiff = = 2):        Print "Computer wins!"    elif(diff = = 3ordiff = = 4ordiff = =-1ordiff = =-2):        Print "Player wins!"    elif(diff = =0):Print "Player and Computer tie!"    Else:        Print "Error"    #Test your Code-these CALLS must be PRESENT in your submitted codeRpsls ("Rock") Rpsls ("Spock") Rpsls ("Paper") Rpsls ("Lizard") Rpsls ("Scissors")#Always remember-to- check your completed program against the grading rubric

Coursera-an Introduction to Interactive programming in Python (Part 1)-mini-project-rock-paper-scissors-lizard-spock

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.