• About Me




    Lily@Melbourne, Australia

    Login

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

Python-Mini-project (Week1)

在看程乃珊的某本书里,知道“石头剪刀布”这个我们似乎无师自通的游戏是正宗的舶来品,然后在参加Coursera的课程后知道还有这样一个版本,挺有意思的,可惜我找不到老师一样的老顽童,而且我脑容量有限,3个以上就有点稀里糊涂。不瞒你说,我是用枚举法写下不同的求余结果,才真正弄懂这道题。我不是那种一点就通透的机灵人,不过好在勤奋,知难而上;)

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

我在Coursera不参加meet-up,难得参与一次讨论,自顾自的冥思苦想,即便辗转反侧不得其解,也懒得请教别人,它只是满足我对名校的一种发自内心的热烈渴望,和扩大交际圈收集一张张证书没有关系。

Rock-paper-scissors-lizard-Spock

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.

Code:

效果在这里>>

# 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”
else:
#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
else:
#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!”
else:
print “Player and computer tie!”

# print results
print “”

# test your code
rpsls(“rock”)
rpsls(“Spock”)
rpsls(“paper”)
rpsls(“lizard”)
rpsls(“scissors”)

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

 

Advertisements

发表评论

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 博主赞过: