python实现猜拳小游戏

yipeiwu_com7年前Python基础

用python实现猜拳小游戏,供大家参考,具体内容如下

本练习旨在养成良好的编码习惯和练习逻辑思考.

1、使用python版本: 3.7.3;

2、代码内容实现如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
简单实现猜拳小游戏,默认每回合 五局
Version: 0.1
Author: smartbabble
Date: 2018-03-12
"""

from random import randint

def mora_game():
 Rounds = 0
 Flag = True
 while Flag and Rounds < 5:
  robot = randint(1,3)
  player = input("游戏开始请出招:"
      "1(表示剪刀),"
      "2(表示石头),"
      "3(表示布),"
      "q或Q(表示退出游戏) \n")
  if player.lower() == "q" :
   Flag = False
   print("游戏终止!")
  else:
   player = int(player)
   Rounds += 1
   if robot == player:
    print("打平!")
   else:
    print("%s 赢得本局" % ("robot"
         if robot-player == 1 else "player"))

def main():
 mora_game()

if __name__ == '__main__':
 main()

执行运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python正则获取、过滤或者替换HTML标签的方法

本文实例介绍了Python通过正则表达式获取,去除(过滤)或者替换HTML标签的几种方法,具体内容如下 python正则表达式关键内容: python正则表达式转义符: . 匹配除...

python executemany的使用及注意事项

使用executemany对数据进行批量插入的话,要注意一下事项: #coding:utf8 conn = MySQLdb.connect(host = “localhost”, u...

解析Python的缩进规则的使用

Python中的缩进(Indentation)决定了代码的作用域范围。这一点和传统的c/c++有很大的不同(传统的c/c++使用花括号{}符,python使用缩进空格)。 每行代码中开头...

Python安装官方whl包和tar.gz包的方法(推荐)

Windows环境:   安装whl包:pip install wheel    ->    pip install&n...

Python中时间datetime的处理与转换用法总结

python中日期类datetime功能比较强大,使用起来很方便,把常用的两种用法总结如下: from datetime import datetime from datetime...