作业week1 'Rock-paper-scissors-lizard-Spock'
程序员文章站
2022-06-06 08:30:05
...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = 'Rock-paper-scissors-lizard-Spock'
__author__ = 'Steve'
__mtime__ = '2017/9/23'
"""
# The key idea of this program is to equate the strings
# "rock", "papper", "scissors", "lizard", "Spock" to numbers
# as follows:
#
# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors
import random
def name_to_number(name):
if name == "rock":
return 0
elif name == "Spock":
return 1
elif name == "paper":
return 2
elif name == "lizard":
return 3
elif name == "scissors":
return 4
else:
return "Error"
def number_to_name(number):
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"
def rpsls(name):
player_number = name_to_number(name)
computer_number = random.randrange(0, 5)
difference = (player_number - computer_number) % 5
print "Player choose", name
print "Computer chooses", number_to_name(computer_number)
if difference == 0:
print "Player and Computer tie!"
elif difference == 1:
print "Player Win!"
elif difference == 2:
print "Player Win!"
elif difference == 3:
print "Computer Wins!"
else:
print "Computer Wins!"
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")