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实现图片拼接的代码

具体代码如下所示: import os from PIL import Image UNIT_SIZE = 220 # the size of image save_path = '...

详解Python3 基本数据类型

详解Python3 基本数据类型

Python3 基本数据类型 Python 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。 在 Python 中,变量就是变量,它没有类型,我们所说的"类...

CentOS中使用virtualenv搭建python3环境

问题描述 环境: CentOS6.5 想在此环境下使用python3进行开发,但CentOS6.5默认的python环境是2.6.6版本。 之前的做法是直接从源码安装python3,...

python 获取页面表格数据存放到csv中的方法

获取单独一个table,代码如下: #!/usr/bin/env python3 # _*_ coding=utf-8 _*_ import csv from urllib.requ...

Pandas中Series和DataFrame的索引实现

正文 在对Series对象和DataFrame对象进行索引的时候要明确这么一个概念:是使用下标进行索引,还是使用关键字进行索引。比如list进行索引的时候使用的是下标,而dict索引的时...