python脚本编写的人机猜拳小游戏

简介:

coding: utf8

import random

all_choices = ['石头', '剪刀', '布']
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
prompt = """(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): """
pwin = 0
cwin = 0

while pwin < 2 and cwin < 2:
computer = random.choice(all_choices)
try:
ind = int(raw_input(prompt))
player = all_choices[ind]
except (ValueError, IndexError):
print 'Inavlid input. Try again.'
continue
except (KeyboardInterrupt, EOFError):
print '\nBye-bye'
break

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:
    pwin += 1
    print '\033[31;1mYou WIN!!!\033[0m'
else:
    cwin += 1
    print '\033[31;1mYou LOSE!!!\033[0m'









本文转自 英强云计算 51CTO博客,原文链接:http://blog.51cto.com/13404755/2052845,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
2月前
|
Linux Shell Python
Linux执行Python脚本
Linux执行Python脚本
30 1
|
2月前
|
Python
python小游戏7
python小游戏7
|
2月前
|
Python
python小游戏6
python小游戏6
|
2月前
|
Python
python小游戏5
python小游戏5
|
2月前
|
Python
python小游戏4
python小游戏4
|
2月前
|
Python
python小游戏1
python小游戏1
|
2月前
|
Python
python小游戏3
python小游戏3
|
12天前
|
Shell Python
python|闲谈2048小游戏和数组的旋转及翻转和转置
python|闲谈2048小游戏和数组的旋转及翻转和转置
25 1
|
26天前
|
JSON 测试技术 持续交付
自动化测试与脚本编写:Python实践指南
【4月更文挑战第9天】本文探讨了Python在自动化测试中的应用,强调其作为热门选择的原因。Python拥有丰富的测试框架(如unittest、pytest、nose)以支持自动化测试,简化测试用例的编写与维护。示例展示了使用unittest进行单元测试的基本步骤。此外,Python还适用于集成测试、系统测试等,提供模拟外部系统行为的工具。在脚本编写实践中,Python的灵活语法和强大库(如os、shutil、sqlite3、json)助力执行复杂测试任务。同时,Python支持并发、分布式执行及与Jenkins、Travis CI等持续集成工具的集成,提升测试效率和质量。