1
2
3
4
5
6
7
8
9
10
|
import random
player_input = input ( "请输入(0剪刀,1石头,2布):" )
player = int (player_input)
computer = random.randint( 0 , 2 )
if (player = = 0 and computer = = 2 ) or (player = = 1 and computer = = 0 ) or (player = = 2 and computer = = 1 ):
print ( "电脑出的是%s,恭喜,你赢了!" % computer)
elif (player = = 0 and computer = = 0 ) or (player = = 1 and computer = = 1 ) or (player = = 2 and computer = = 2 ): #将此行代替为(player==computer):更加简便
print ( "电脑出的是%s,打成了平局" % computer)
else :
print ( "电脑出的是%S,你输了!" % computer)
|
通过本代码我们可以进而写出猜筛子游戏,通过电脑给玩家随机给出的数字与电脑随机出一个数字进行比较。
猜筛子游戏
1
2
3
4
5
6
7
8
9
10
11
|
import random
#player_input=input("请输入1~6其中一个数:")此行代码是玩家手动输入一个数与电脑比大小
player = random.randint( 1 , 6 ) #int(player_input)
print ( "你的筛子摇出了%s" % player)
computer = random.randint( 1 , 6 )
if (player>computer):
print ( "电脑出的是%s,你赢了!" % computer)
elif (player = = computer):
print ( "电脑出的是%s,打成了平局" % computer)
else :
print ( "电脑出的是%s,你输了!" % computer)
|
1
2
3
|
import random
num = random.randint(a,b)
print (num) * * * #随机生成一个a到b的数字***
|
总结
到此这篇关于Python中猜拳游戏与猜筛子游戏的实现方法就介绍到这了,更多相关Python猜拳游戏与猜筛子游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_44364779/article/details/108393500