python课后练习 剪刀石头布小游戏

特别特别繁琐的剪刀石头布。。。

import random
items=['rock','paper','scissors']										#电脑可能出的:剪刀,石头或者布
winrounds=0;															#初始化赢的局数
loserounds=0;															#初始化输的局数
ifContinue='yes'														#用于结束后判断是否再来一局

while ifContinue=='yes':												#是yes的情况下运行

	for rounds in range(1,4) :											#一共3局
		print("1.rock 2.paper 3.scissors.\ninput number:")				
		my=int(input())													#从键盘读取用户输入的数字,代表剪刀石头或者布
		bot=random.choice(items);										#电脑随机出
		if my==1 and bot=='scissors':									#下面是所有输赢情况,写的有点太繁琐,不知道日后会不会想出更加简洁的写法
			print("\nround "+str(rounds)+" :\n"+str(bot)+". you win!\n")
			winrounds+=1												#赢的时候,赢的局数+1,输的时候,输的局数+1,平局不计
		elif my==1 and bot=='paper':
			print("\nround "+str(rounds)+" :\n"+str(bot)+". you lose!\n")
			loserounds+=1
		elif my==1 and bot=='rock':
			print("\nround "+str(rounds)+" :\n"+str(bot)+". we are even.\n")
			
		elif my==2 and bot=='scissors':
			print("\nround "+str(rounds)+" :\n"+str(bot)+". you lose!\n")
			loserounds+=1
		elif my==2 and bot=='paper':
			print("\nround "+str(rounds)+" :\n"+str(bot)+". we are even.\n")
		elif my==2 and bot=='rock':
			print("\nround "+str(rounds)+" :\n"+str(bot)+". you win!\n")
			winrounds+=1
			
		elif my==3 and bot=='scissors':
			print("\nround "+str(rounds)+" :\n"+str(bot)+". we are even.\n")
		elif my==3 and bot=='paper':
			print("\nround "+str(rounds)+" :\n"+str(bot)+". you win!\n")
			winrounds+=1
		elif my==3 and bot=='rock':
			print("\nround "+str(rounds)+" :\n"+str(bot)+". you lose!\n")
			loserounds+=1
			
	print("game over!\nyou win "+str(winrounds)+" rounds. you lose "+ str(loserounds)+" rounds.")
	if winrounds>loserounds : 											#3局2胜制
		print("WINNER")
	elif winrounds<loserounds :
		print("LOSER")
	elif winrounds==loserounds :
		print("PEACE")
	
	print("start over?(yes/no)")										#判断是否再来一局
	ifContinue=str(input())

	if ifContinue=='no':												#输入no就结束,break退出循环体,输入yes就继续
		print("alright bye!")
		break
	elif ifContinue=='yes':
		print("here we go!\n")



	

	

猜你喜欢

转载自blog.csdn.net/csdn317341539/article/details/88777777