[Python学习阶段性总结] Rock-paper-scissors-lizard-Spock游戏 Mini-project #1

很高兴今天早晨又做出来RPSLS小游戏,我会继续努力!

“RPSLS”游戏全称为Rock-paper-scissors-lizard-Spock,剪刀-石头-布的升级版。为了降低平手概率,增添了2个角色:Spock和蜥蜴君。

游戏规则是:每个都能赢2个输2个并与自己持平。在这里,剪刀能剪纸和蜥蜴;纸可以包住石头,驳斥Spock;石头击烂蜥蜴和剪刀;蜥蜴可以毒害Spock,吃纸;最后,Spock可以砸碎剪刀,使石头蒸发。

Mini-project描述:https://www.coursera.org/learn/interactive-python-1/supplement/ijRP5/mini-project-description

收获:

原来编程,最重要的是能静下心来分析。如果感到棘手,可以休息会儿,补足精力再战,就真的可以克服呢!


这是我的解答,若有更好的建议,欢迎指点~~

#RPSLS
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:
		print('Please input name among:"rock", "paper", "scissors", "lizard", or "Spock".')

def number_to_name(number):
	if number in range(0,5):
		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:
		print('Please input number n the range 0 to 4.')

def rpsls(player_choice):
	print()
	print('Player chooses',player_choice)
	player_number = name_to_number(player_choice)
	comp_number = random.randrange(0,5)
	comp_choice = number_to_name(comp_number)
	print('Computer chooses',comp_choice)
	if player_number - comp_number in range(-4,-2) or range(1,3):
		print('Player wins!')
	elif player_number - comp_number in range(-2,0) or range(3,5):
		print('Computer wins!')
	elif player_number - comp_number == 0:
		print('Tie!')
	else:
		print('Error!')

print('"Rock-paper-scissors-lizard-Spock" game Start!')
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")



你可能感兴趣的:(游戏,代码,python,学习,Coursera)