python实现猜拳小游戏

yipeiwu_com6年前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后端接收前端回传的文件方法

如下所示: filename=None     if request.method == 'POST' and request.FILES.get('...

python之django母板页面的使用

其实就是利用{% block xxx %}   {% endblock %}的方式定义一个块,相当于占位。存放在某个html中,比如base.html 然后在需要实现...

Python 2.7中文显示与处理方法

在学习使用Python的过程中,一定会遇到文字输入与处理,这就不可避免的会使用中文字符。但是Python2.7默认使用的字符集是ASCII,并不支持中文字符的显示与处理,因些如果要在Py...

利用Python演示数型数据结构的教程

使用 Python 内建的defaultdict 方法可以轻松定义一个树的数据结构。 简单的说树也可以是一个字典数据结构   def tree(): return def...

Python 类属性与实例属性,类对象与实例对象用法分析

Python 类属性与实例属性,类对象与实例对象用法分析

本文实例讲述了Python 类属性与实例属性,类对象与实例对象用法。分享给大家供大家参考,具体如下: demo.py(类属性,所有实例对象共用类属性): # 定义工具类 继...