python简易计算器程序(Python 计算器)

python简易计算器程序(Python 计算器)1、打开idle。点击file,然后点击new file.这是创建一个新的文件。

本文目录一览:

如何使用python编程写一个加法计算器

1、打开idle。点击file,然后点击new file.这是创建一个新的文件。

新建一个文件之后,我们输入第一行代码,使用print函数,在屏幕上打印一句话,其中字符串要使用双引号,输入法要使用英文输入法,如果符号使用中文输入法输入,就会出现错误。print(“我们做一个两个整数相加的计算题!”)

同理,在屏幕上打印第二句话,与用户交互,提醒用户输入第一个数。

第三行调用input函数,将用户输入的内容赋值给a,这时候a收到的是字符串信息,所以需要下一步把字符串转换为整型。这输入计算机处理数据指令。

然后依照以上的步骤写第二个加数,和最后输出的和,注意最后一句打印结果时,引号内部是字符串形式,x+y是数值形式,所以需要在中间加上一个逗号。如果不加逗号就会提示错误信息,以上就是所有的程序编写完成,下一步就开始保存,命名,运行。如图所示

运行结果如下:

更多Python相关技术文章,请访问Python教程栏目进行学习!以上就是小编分享的关于如何使用python编程写一个加法计算器的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!

用PYTHON2做个计算器,只要加减乘除

”’

命令行简易计算器

”’

import sys

class culate():

#加法

def add(self,a,b):

return a+b

#减法

def mut(self,a,b):

return a-b

#乘法

def sub(self,a,b):

return a*b

#除法

def mod(self,a,b):

return a/b

c=culate()

while True:

n=input(“请选择你的操作:\n1.加法\n2.减法\n3.乘法\n4.除法\n0.退出\n”)

if n==”0″:

break

elif n==”1″:

a=input(“请输入第一个数:”)

b=input(“请输入第二个数:”)

print(c.add(int(a),int(b)))

continue

elif n==”2″:

a=input(“请输入第一个数:”)

b=input(“请输入第二个数:”)

print(c.mut(int(a),int(b)))

continue

elif n==”3″:

a=input(“请输入第一个数:”)

b=input(“请输入第二个数:”)

print(c.sub(int(a),int(b)))

continue

elif n==”4″:

a=input(“请输入第一个数:”)

b=input(“请输入第二个数:”)

print(c.mod(int(a),int(b)))

continue

”’

结果:

请选择你的操作:

1.加法

2.减法

3.乘法

4.除法

0.退出

3

请输入第一个数:9

请输入第二个数:3

27

请选择你的操作:

1.加法

2.减法

3.乘法

4.除法

0.退出

4

请输入第一个数:9

请输入第二个数:3

3.0

请选择你的操作:

1.加法

2.减法

3.乘法

4.除法

0.退出

”’

用python操作Windows的计算器。

安装pywin32模块。

注意:乘法的优先级高,在计算器输入时要加括号!

代码:

import win32api,win32gui, win32con

import win32com.client

shell = win32com.client.Dispatch(“WScript.Shell”)

shell.Run(“calc”)

win32api.Sleep(1000)

shell.SendKeys(“200{+}”)

win32api.Sleep(1000)

shell.SendKeys(“{(}100\x2a2{)}”)

win32api.Sleep(1000)

shell.SendKeys(“-22”)

win32api.Sleep(1000)

shell.SendKeys(“=”)

h = win32gui.FindWindow(“SciCalc”, None)

edit = win32gui.FindWindowEx(h, None, ‘Edit’, None)

bufLen = 1024

buf = win32gui.PyMakeBuffer(bufLen)

n = win32gui.SendMessage(edit, win32con.WM_GETTEXT, bufLen, buf)

print buf[0:n]

win32api.Sleep(1000)

win32gui.SendMessage(h, win32con.WM_SYSCOMMAND, win32con.SC_CLOSE, 0);

运行结果:

378.

python简易计算器程序(Python 计算器)

如何用 Python 写一个带 GUI 的科学计算程序

使用Tkinter图形库,如果你是用的linux系统 记得将第一行改为from tkinter import *

这个代码实现的挺简单,并不是很复杂的科学计算器界面,你可以以此为基础,添加自己想要的东西:给你个截图:

代码是如下, 我就不给你添注释了啊:

#!/usr/bin/env python3.4

from Tkinter import *

import parser

root = Tk()

root.title(‘Calculator’)

i = 0

def factorial():

“””Calculates the factorial of the number entered.”””

whole_string = display.get()

number = int(whole_string)

fact = 1

counter = number

try:

while counter 0:

fact = fact*counter

counter -= 1

clear_all()

display.insert(0, fact)

except Exception:

clear_all()

display.insert(0, “Error”)

def clear_all():

“””clears all the content in the Entry widget”””

display.delete(0, END)

def get_variables(num):

“””Gets the user input for operands and puts it inside the entry widget”””

global i

display.insert(i, num)

i += 1

def get_operation(operator):

“””Gets the operand the user wants to apply on the functions”””

global i

length = len(operator)

display.insert(i, operator)

i += length

def undo():

“””removes the last entered operator/variable from entry widget”””

whole_string = display.get()

if len(whole_string): ## repeats until

## now just decrement the string by one index

new_string = whole_string[:-1]

print(new_string)

clear_all()

display.insert(0, new_string)

else:

clear_all()

display.insert(0, “Error, press AC”)

def calculate():

“””

Evaluates the expression

ref :

“””

whole_string = display.get()

try:

formulae = parser.expr(whole_string).compile()

result = eval(formulae)

clear_all()

display.insert(0, result)

except Exception:

clear_all()

display.insert(0, “Error!”)

root.columnconfigure(0,pad=3)

root.columnconfigure(1,pad=3)

root.columnconfigure(2,pad=3)

root.columnconfigure(3,pad=3)

root.columnconfigure(4,pad=3)

root.rowconfigure(0,pad=3)

root.rowconfigure(1,pad=3)

root.rowconfigure(2,pad=3)

root.rowconfigure(3,pad=3)

display = Entry(root, font = (“Calibri”, 13))

display.grid(row = 1, columnspan = 6 , sticky = W+E)

one = Button(root, text = “1”, command = lambda : get_variables(1), font=(“Calibri”, 12))

one.grid(row = 2, column = 0)

two = Button(root, text = “2”, command = lambda : get_variables(2), font=(“Calibri”, 12))

two.grid(row = 2, column = 1)

three = Button(root, text = “3”, command = lambda : get_variables(3), font=(“Calibri”, 12))

three.grid(row = 2, column = 2)

four = Button(root, text = “4”, command = lambda : get_variables(4), font=(“Calibri”, 12))

four.grid(row = 3 , column = 0)

five = Button(root, text = “5”, command = lambda : get_variables(5), font=(“Calibri”, 12))

five.grid(row = 3, column = 1)

six = Button(root, text = “6”, command = lambda : get_variables(6), font=(“Calibri”, 12))

six.grid(row = 3, column = 2)

seven = Button(root, text = “7”, command = lambda : get_variables(7), font=(“Calibri”, 12))

seven.grid(row = 4, column = 0)

eight = Button(root, text = “8”, command = lambda : get_variables(8), font=(“Calibri”, 12))

eight.grid(row = 4, column = 1)

nine = Button(root , text = “9”, command = lambda : get_variables(9), font=(“Calibri”, 12))

nine.grid(row = 4, column = 2)

cls = Button(root, text = “AC”, command = clear_all, font=(“Calibri”, 12), foreground = “red”)

cls.grid(row = 5, column = 0)

zero = Button(root, text = “0”, command = lambda : get_variables(0), font=(“Calibri”, 12))

zero.grid(row = 5, column = 1)

result = Button(root, text = “=”, command = calculate, font=(“Calibri”, 12), foreground = “red”)

result.grid(row = 5, column = 2)

plus = Button(root, text = “+”, command = lambda : get_operation(“+”), font=(“Calibri”, 12))

plus.grid(row = 2, column = 3)

minus = Button(root, text = “-“, command = lambda : get_operation(“-“), font=(“Calibri”, 12))

minus.grid(row = 3, column = 3)

multiply = Button(root,text = “*”, command = lambda : get_operation(“*”), font=(“Calibri”, 12))

multiply.grid(row = 4, column = 3)

divide = Button(root, text = “/”, command = lambda : get_operation(“/”), font=(“Calibri”, 12))

divide.grid(row = 5, column = 3)

# adding new operations

pi = Button(root, text = “pi”, command = lambda: get_operation(“*3.14”), font =(“Calibri”, 12))

pi.grid(row = 2, column = 4)

modulo = Button(root, text = “%”, command = lambda : get_operation(“%”), font=(“Calibri”, 12))

modulo.grid(row = 3, column = 4)

left_bracket = Button(root, text = “(“, command = lambda: get_operation(“(“), font =(“Calibri”, 12))

left_bracket.grid(row = 4, column = 4)

exp = Button(root, text = “exp”, command = lambda: get_operation(“**”), font = (“Calibri”, 10))

exp.grid(row = 5, column = 4)

## To be added :

# sin, cos, log, ln

undo_button = Button(root, text = “-“, command = undo, font =(“Calibri”, 12), foreground = “red”)

undo_button.grid(row = 2, column = 5)

fact = Button(root, text = “x!”, command = factorial, font=(“Calibri”, 12))

fact.grid(row = 3, column = 5)

right_bracket = Button(root, text = “)”, command = lambda: get_operation(“)”), font =(“Calibri”, 12))

right_bracket.grid(row = 4, column = 5)

square = Button(root, text = “^2”, command = lambda: get_operation(“**2”), font = (“Calibri”, 10))

square.grid(row = 5, column = 5)

root.mainloop()

如何运用Python编写简易计算器

import time

print(“计算器”)

print(“+等于加法模式 -等于减法模式 *等于乘法模式 /等于除法模式”)

while 2 1:

try:

print(“请输入+,-,*或/”)

a = input()

if a == “+”:

print(“请输入第1个加数”)

b = input()

print(“请输入第2个加数”)

c = input()

print(“计算中”)

time.sleep(0.3)

j = float(b) + float(c)

print(“等于”+str(j))

elif a == “-“:

print(“请输入被减数”)

b = input()

print(“请输入减数”)

c = input()

print(“计算中”)

time.sleep(0.3)

j = float(b) – float(c)

print(“等于”+str(j))

elif a == “*”:

print(“请输入第1个因数”)

b = input()

print(“请输入第2个因数”)

c = input()

print(“计算中”)

time.sleep(0.3)

j = float(b) * float(c)

print(“等于”+str(j))

elif a == “/”:

print(“……等于余数模式 .等于小数模式”)

print(“请输入……或.”)

a = input()

if a == “.”:

print(“请输入被除数”)

b = input()

print(“请输入除数”)

c = input()

print(“计算中”)

time.sleep(0.3)

j = float(b) / float(c)

print(“等于”+str(j))

if c == “0”:

print(“除数不能为0!”)

elif a == “……”:

print(“请输入被除数”)

b = input()

print(“请输入除数”)

c = input()

j = float(b) // float(c)

e = float(b) % float(c)

print(“等于”+str(j)+”……”+str(e))

if c == “0”:

print(“除数不能为0!”)

except Exception as e:

print(“您输入的内容有错误”)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/23393.html

(0)
上一篇 2023-10-29 18:30
下一篇 2023-10-29

相关推荐

  • Python身份运算符:用于比较对象的身份

    Python身份运算符:用于比较对象的身份在Python中,使用身份运算符来比较两个对象的身份,常用的身份运算符包括“is”和“is not”。

    2024-03-29
    23
  • mysql 性能调优_MySQL数据库优化

    mysql 性能调优_MySQL数据库优化EXPLAIN 首先祭出官方文档(这是5.7的,请自行选择版本): Understanding the Query Execution Plan 英文不想看,就看这篇吧: 全网最全 | MySQL E

    2023-05-16
    110
  • mysql索引笔记

    mysql索引笔记MYSQL索引 一、索引的优缺点 优点: 1.通过创建唯一索引,可以保证数据库表中每一行的唯一性。 2.可以大大加快查询速度,这是创建索引的最主要原因 3.在实现数据参考完整性方面,可以加速表和表之间

    2022-12-27
    94
  • PostgreSQL源码学习–插入数据#3

    PostgreSQL源码学习–插入数据#3本节介绍heapam_tuple_insert函数的代码流程 相关数据结构 结构体中有些成员可能目前难以理解,暂时先列出来,先把当前用到的成员能搞明白就可以。 // src/include/exec…

    2023-02-18
    101
  • 内建质量,你真的了解么?「建议收藏」

    内建质量,你真的了解么?「建议收藏」内建质量定义 内建质量作用在开发过程中,要求软件生命周期之间参与的各个角色都需要实时的对软件的质量负责。确保软件在交付到下一环节前已经有了基础的质量保证。其核心目的就是减少因为质量问题导致的返工,而…

    2023-02-26
    105
  • Python中常用的列表操作

    Python中常用的列表操作a href=”https://www.python100.com/a/sm.html”font color=”red”免责声明/font/a a href=”https://beian.miit.gov.cn/”苏ICP备2023018380号-1/a Copyright www.python100.com .Some Rights Reserved.

    2023-12-13
    58
  • 第一章初识_Mysql教程

    第一章初识_Mysql教程MySQL理论 1. 数据库 数据: 数据就是一种符号,记录人类认为有价值的东西,例如图片、视频、文字、表格等 从计算机角度来看,就是二进制、16进制的文件 数据库的分类: RDBMS:关系型数据库…

    2022-12-27
    105
  • 分享一份关于Hadoop2.2.0集群环境搭建文档

    分享一份关于Hadoop2.2.0集群环境搭建文档[TOC] 一,准备环境 基本配置如下: 初始化四台 虚拟机,配置如下: 修改系统时区 为方便使用建议如下配置: 安装 ‘ ‘ 插件; 设置 行号; 安装 插件服务; 安装 插件服务并加以配置,方便文

    2022-12-18
    97

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注