Python实现剪刀石头布小游戏(与电脑对战)

yipeiwu_com5年前Python基础

具体代码如下所述:

srpgame.py
#!/urs/bin/env python
import random
all_choice = ['石头','剪刀','布']
win_list = [['石头','剪刀'],['剪刀','布'],['布','石头']]
prompt = """
(0) 石头
(1) 剪刀
(2) 布
Please input your choice(0/1/2): """
computer = random.choice(all_choice)
ind = int(input(prompt))
player = all_choice[ind]
print("Your choice: %s,Computer's choice: %s" %(player,computer))
if player == computer:
print('\033[32;1m平局\033[0m')
elif [player,computer] in win_list:
print('\033[31;1mYou WIN!!!\033[0m')
else:
print('\033[31;1mYou LOSE!!!\033[0m')
演示
D:\Python scripts 
λ python3 .\srpgame.py
(0) 石头 
(1) 剪刀 
(2) 布 
Please input your choice(0/1/2): 0 
Your choice: 石头,Computer's choice: 剪刀 
You WIN!!! 
D:\Python scripts 
λ python3 .\srpgame.py
(0) 石头 
(1) 剪刀 
(2) 布 
Please input your choice(0/1/2): 1 
Your choice: 剪刀,Computer's choice: 剪刀 
平局 
D:\Python scripts 
λ python3 .\srpgame.py
(0) 石头 
(1) 剪刀 
(2) 布 
Please input your choice(0/1/2): 2 
Your choice: 布,Computer's choice: 石头 
You WIN!!! 
D:\Python scripts 
λ python3 .\srpgame.py
(0) 石头 
(1) 剪刀 
(2) 布 
Please input your choice(0/1/2): 0 
Your choice: 石头,Computer's choice: 布 
You LOSE!!!

总结

以上所述是小编给大家介绍的Python实现剪刀石头布小游戏(与电脑对战),希望对大家有所帮助!

相关文章

python获取代码运行时间的实例代码

有的时候,操作大文件,或者取数,要很久,我们给脚本首尾添加一段代码就知道,这段代码整体的大致运行时间了。 import time start =time.clock() #中间写上代...

Python 中urls.py:URL dispatcher(路由配置文件)详解

Python 中urls.py:URL dispatcher(路由配置文件)详解

urls.py:URL dispatcher(路由配置文件) URL配置(URLconf)就像是Django所支撑网站的目录。它的本质是URL模式以及要为该URL模式调用的视图函数之间的...

Python生成个性签名图片获取GUI过程解析

Python生成个性签名图片获取GUI过程解析

这篇文章主要介绍了Python生成个性签名图片获取GUI过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 先来看看程序运行的样子...

Python 加密的实例详解

 Python 加密的实例详解 hashlib支持md5,sha1,sha256,sha384,sha512,用法和md5一样  import hashlib...

Python简单计算给定某一年的某一天是星期几示例

Python简单计算给定某一年的某一天是星期几示例

本文实例讲述了Python简单计算给定某一年的某一天是星期几。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #计算某特定天使星期几 #蔡勒公式:w=...