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的Django框架中使用SQLAlchemy操作数据库的教程

零、SQLAlchemy是什么? SQLAlchemy的官网上写着它的介绍文字: SQLAlchemy is the Python SQL toolkit and Object Rela...

Python实现的简单模板引擎功能示例

本文实例讲述了Python实现的简单模板引擎功能。分享给大家供大家参考,具体如下: #coding:utf- 8 __author__="sdm" __author_email='s...

浅析Python中元祖、列表和字典的区别

1、列表(list)   list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。   列表中的项目应该包括在方括号中,这样Python就知道你是指明一个列表。一旦...

对python打乱数据集中X,y标签对的方法详解

对python打乱数据集中X,y标签对的方法详解

今天踩过的两个小坑: 一.用random的shuffle打乱数据集中的数据-标签对 index=[i for i in range(len(X_batch))] # print(ty...

CentOS 6.5中安装Python 3.6.2的方法步骤

前言 centos 是自带python的。但是版本稍微旧一些。搞python开发,肯定要用新一点的稳定版。所以,要升级一下python。本文将介绍在CentOS 6.5中安装Python...