python概率计算器实例分析

本文实例讲述了python概率计算器实现方法。分享给大家供大家参考。具体实现方法如下:

from random import randrange
#randrange form random module
def calc_prob(strengths):
  """A function that receives an array of two numbers
  indicating the strength of each party
  and returns the winner"""
  if strengths[1]>strengths[0]:
#Bring the bigger number to the first position in the array
    temp=strengths[0]
    strengths[0]=strengths[1]
    strengths[1]=temp
  prob1=abs(strengths[0]-strengths[1])
#The relative strength of the 2 parties
  prob2=randrange(0,100)
#To calculate the luck that decides the outcome
  if prob2 in range(0,33-prob1):
#Check if the weaker party is capable of winning.
#The condition gets narrower with the increase
#in relative strengths of each parties
    return strengths[1]
  elif prob2 in range(33-prob1,66-prob1):
  #The middle condition
    return "Draw"
  else:
     return strengths[0]
#Luck favors the stronger party and if relative strength
#between the teams is too large,
#the match ends up in favor of the stronger party
#Example
calc_prob([50,75]);#Always has to be a list to allow exchange
#Can be programmed in hundreds of better ways. Good luck!

希望本文所述对大家的Python程序设计有所帮助。

(0)

相关推荐

  • Python实现的科学计算器功能示例

    本文实例讲述了Python实现的科学计算器功能.分享给大家供大家参考,具体如下: import wx import re import math # begin wxGlade: extracode # end wxGlade ans=0 ts="" class MyFrame(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: MyFrame.__init__ kwds["style"] =

  • python正则表达式之作业计算器

    作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式,运算后得出结果,结果必须与真实的计算器所得出的结果一致 一.说明: 有一点bug就是不能计算幂次方,如:'6**6'会报错 该计算器思路: 1.没用使用递归,先找出并计算所有括号里的公式,再计算乘除

  • Python实现简单的四则运算计算器

    一.算法 1.算法的主要思想就是将一个中缀表达式(Infix expression)转换成便于处理的后缀表达式(Postfix expression),然后借助于栈这个简单的数据结构,计算出表达式的结果. 2.关于如何讲普通的表达式转换成后缀表达式,以及如何处理后缀表达式并计算出结果的具体算法描述不在此叙述了,书上有详细的说明. 二.简易计算器 使用说明 使用该计算器类的简单示例如下: # usage c = Calculator() print('result: {:f}'.formart(c

  • Python设计实现的计算器功能完整实例

    本文实例讲述了Python设计实现的计算器功能.分享给大家供大家参考,具体如下: 通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3)/(16-3*2)) 我的处理计算基本思路是: 解题思路是,需要优先处理内层括号运算--外层括号运算--先乘除后加减的原则: 1.正则处理用户输入的字符串,然后对其进行判断,判断计算公式是否有括号,有就先将计算公式进

  • 基于wxpython开发的简单gui计算器实例

    本文实例讲述了基于wxpython开发的简单gui计算器.分享给大家供大家参考.具体如下: # wxCalc1 a simple GUI calculator using wxPython # created with the Boa Constructor which generates all the GUI components # all I had to do is add some code for each button click event # Boa free from: h

  • 仅用50行代码实现一个Python编写的计算器的教程

     简介 在这篇文章中,我将向大家演示怎样向一个通用计算器一样解析并计算一个四则运算表达式.当我们结束的时候,我们将得到一个可以处理诸如 1+2*-(-3+2)/5.6+3样式的表达式的计算器了.当然,你也可以将它拓展的更为强大. 我本意是想提供一个简单有趣的课程来讲解 语法分析 和 正规语法(编译原理内容).同时,介绍一下PlyPlus,这是一个我断断续续改进了好几年的语法解析 接口.作为这个课程的附加产物,我们最后会得到完全可替代eval()的一个安全的四则运算器. 如果你想在自家的电脑上试试

  • python3.5 + PyQt5 +Eric6 实现的一个计算器代码

    目前可以实现简单的计算.计算前请重置,设计的时候默认数字是0,学了半天就做出来个这么个结果,bug不少. python3.5 + PyQt5 +Eric6 在windows7 32位系统可以完美运行 计算器,简单学了半天就画个图实现的存在bug,部分按钮还未实现,后续优化. 代码结构如图: jisuan.py import re #匹配整数或小数的乘除法,包括了开头存在减号的情况 mul_div=re.compile("(-?\d+)(\.\d+)?(\*|/)(-?\d+)(\.\d+)?&q

  • python 简易计算器程序,代码就几行

    代码: 复制代码 代码如下: import os while True: dynamic = input('输入计算表达式:') if dynamic != 'cls': try: result = eval(dynamic) print('计算结果:'+str(result)) except: print('计算表达式输入有误!') else: command = 'cls' os.system(command)

  • Python 正则表达式实现计算器功能

    需求: 用户输入运算表达式,终端显示计算结果 代码: # !/usr/bin/env/ python3 # -*- coding: utf-8 -*- """用户输入计算表达式,显示计算结果""" __author__ = 'Jack' import re bracket = re.compile(r'\([^()]+\)') # 寻找最内层括号规则 mul = re.compile(r'(\d+\.?\d*\*-\d+\.?\d*)|(\d+\

  • python3.5仿微软计算器程序

    本文实例为大家分享了python3.5仿微软计算器的具体代码,供大家参考,具体内容如下 from tkinter import * from math import * root = Tk() root.title("计算器") root.geometry("1200x260+0+0") def come(event): event.widget["background"]= "orange"#event的widget方法,恩

  • Python开发的实用计算器完整实例

    本文实例讲述了Python开发的实用计算器.分享给大家供大家参考,具体如下: 实现功能:图形界面PyQt,输入框,+,-,*,/ :乘方 ,开方 ,取余,清零. 1. Python代码: #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Author : Mr.LiuYC Created on 2014-09-30 E-Mail : liuyanchen0725@gmail.com Introduction: 简易计算器 实现图形界面PyQt,输

随机推荐