• About Me

    Lily@Melbourne, Australia


  • Follow 静水深流 || Still waters run deep on WordPress.com
  • Archives

Python-Mini-project (Week1)


事情有时候就那么凑巧,我借了本python自学,然后就发觉有一所学校正在开课,承上启下正中下怀阿。不象一般地编程课,第一等要务定是要下载相应地软件,一定会碰到各种各样的问题和困难。我遇见过最稀松的版本不相容,系统不兼容,也遇见过最棘手的硬盘不够大或是莫名其妙的乱码,对键盘的无响应。Rice uni的Scott Rixner自己写了一个编程工具,学生只需要一个非IE的浏览器连上网就能play code了,非常喜欢。



Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously throw” one of three hand signals that correspond to rock, paper or 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 that 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 more unlikely.

Rock-paper-scissors-lizard-Spock (RPSLS) is a variant of Rock-paper-scissors that allows five choices. Each choice wins against two other choices, loses against two other choices and ties against itself. Much of RPSLS’s popularity is that it has 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 will build a Python function rpsls(name) that takes as input the string name, which is one of "rock","paper""scissors""lizard", or "Spock". The function then simulates playing a round of Rock-paper-scissors-lizard-Spock by generating its own random choice from these alternatives and then determining the winner using a simple rule that we will next describe.

While Rock-paper-scissor-lizard-Spock has a set of ten rules that logically determine who wins a round of RPSLS, coding up these rules would require a large number (5×5=25) of if/elif/else clauses in your mini-project code. A simpler method for determining the winner is 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 two choices and loses against the following two choices. In all of the mini-projects for this class, I will provide a walk through of the steps involved in building your project to aid its development.



# 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

import random

# helper functions

def number_to_name(number):
# fill in your code below

# convert number to a name using if/elif/else
# don’t forget to return the result!
if number == 0:
name = “rock”
elif number == 1:
name = “Spock”
elif number == 2:
name = “paper”
elif number == 3:
name = “lizard”
elif number == 4:
name = “scissors”
#set the defalut value rock
name = “rock”
return name
def name_to_number(name):
# fill in your code below

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

if name == “rock”:
number = 0
elif name == “Spock”:
number = 1
elif name == “paper”:
number = 2
elif name == “lizard”:
number = 3
elif name == “scissors”:
number = 4
#set the defalut value 0
number = 0
return number

def rpsls(name):

# fill in your code below

# convert name to player_number using name_to_number
player_number = name_to_number(name)
print “Player chooses “+name

# compute random guess for comp_number using random.randrange()
# convert comp_number to name using number_to_name
comp_number = random.randrange(0,5)
comp_guess = number_to_name(comp_number)
print “Computer chooses “+comp_guess

# compute difference of player_number and comp_number modulo five
middle_value = (player_number-comp_number)%5

# use if/elif/else to determine winner
if (middle_value == 4) or (middle_value == 3):
print “Computer wins!”
elif (middle_value == 1) or (middle_value == 2):
print “Player wins!”
print “Player and computer tie!”

# print results
print “”

# test your code

# always remember to check your completed program against the grading rubric




Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: