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整小时 整天时间戳获取算法示例

根据当前时间戳获得整小时时间戳 unit = 3600 start_time = int(time.time())/3600 * 3600 根据当前时间戳获得整天时间戳 uni...

神经网络(BP)算法Python实现及应用

神经网络(BP)算法Python实现及应用

本文实例为大家分享了Python实现神经网络算法及应用的具体代码,供大家参考,具体内容如下 首先用Python实现简单地神经网络算法: import numpy as np #...

在Pycharm中自动添加时间日期作者等信息的方法

在Pycharm中自动添加时间日期作者等信息的方法

1.按照下面路径以此打开 File→→Settings→→Editor→→File and code Templates 右侧找到Python Script,如下图 2.设置相...

Python制作钉钉加密/解密工具

又是很久没有写技术博客了,盖因最近都在学习知识,也没有总结出什么值得分享的内容,所以一直停笔至今。最近的工作和钉钉的开发打上了交到,官方并没有提供任何Python的SDK,于是只能全部自...

CentOS 6.5下安装Python 3.5.2(与Python2并存)

本文主要给大家介绍了关于CentOS 6.5 安装Python 3.5.2并与Python2并存的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: 安装步骤如下 1、准备编译环境...