python编写简单计算器的代码(用python编写计算器程序)

http://www.itjxue.com  2023-02-03 03:19  来源:未知  点击次数: 

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

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

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

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

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

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

运行结果如下:

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

用python编程设计一个简单的计算器程序,要求用户从键盘输入如下形式的表达式:

用exec?函数

小提示:

a?=?3

b?=?4

op?=?'*'?

exec('c?=?%s?%s?%s'%(a,op,b))

print?c

如何用python编写计算器

我想你的需求应该是一个图形界面的程序,而不是简单的在命令行上输入。

那么,要做的第一件事就是选择一个图形界面套件。可以使用原生的TK,也可以用跨平台性能很好的wxPython,或者是整体结构很像MFC的PyWin32。至于pyGTK,pyQT,都是可选的,但是相对来说文档比较少,学习不便。

选定图形库之后,就可以看文档范例了。计算器总体是比较简单的。我记得WxPython的demo里直接就有一个简单计算器,您可以直接取来用。

python计算器

python计算器实现:

wx.TextCtrl(文本输入控件),这个作为计算器的显示屏,点击数字时用AppendText方法将数字添加至文本末尾,要取出数字时用GetValue方法,点击运算符时要将文本框中的数字清空(先存储)用Clear方法然后还有将操作符保存,点击等号后要将第二个操作数存储,然后用int方法将字符串转换为数字,计算后用SetValue方法将结果送入文本框,具体代码如下:

import?wx

#?begin?wxGlade:?extracode

#?end?wxGlade

class?MyFrame(wx.Frame):

????def?__init__(self,?*args,?**kwds):

????????#?begin?wxGlade:?MyFrame.__init__

????????kwds["style"]?=?wx.DEFAULT_FRAME_STYLE

????????wx.Frame.__init__(self,?*args,?**kwds)

????????self.text_ctrl_1?=?wx.TextCtrl(self,?-1,?"",style=wx.TE_READONLY)

????????self.button_37?=?wx.Button(self,?-1,?"1")

????????self.button_38?=?wx.Button(self,?-1,?"2")

????????self.button_39?=?wx.Button(self,?-1,?"3")

????????self.button_40?=?wx.Button(self,?-1,?"+")

????????self.button_41?=?wx.Button(self,?-1,?"4")

????????self.button_42?=?wx.Button(self,?-1,?"5")

????????self.button_43?=?wx.Button(self,?-1,?"6")

????????self.button_44?=?wx.Button(self,?-1,?"-")

????????self.button_46?=?wx.Button(self,?-1,?"7")

????????self.button_45?=?wx.Button(self,?-1,?"8")

????????self.button_47?=?wx.Button(self,?-1,?"9")

????????self.button_48?=?wx.Button(self,?-1,?"x")

????????self.button_49?=?wx.Button(self,?-1,?"C")

????????self.button_50?=?wx.Button(self,?-1,?"0")

????????self.button_51?=?wx.Button(self,?-1,?"=")

????????self.button_52?=?wx.Button(self,?-1,?"/")

????????self.__set_properties()

????????self.__do_layout()

????????self.Bind(wx.EVT_BUTTON,?self.bu1,?self.button_37)

????????self.Bind(wx.EVT_BUTTON,?self.bu2,?self.button_38)

????????self.Bind(wx.EVT_BUTTON,?self.bu3,?self.button_39)

????????self.Bind(wx.EVT_BUTTON,?self.bu_plus,?self.button_40)

????????self.Bind(wx.EVT_BUTTON,?self.bu4,?self.button_41)

????????self.Bind(wx.EVT_BUTTON,?self.bu5,?self.button_42)

????????self.Bind(wx.EVT_BUTTON,?self.bu6,?self.button_43)

????????self.Bind(wx.EVT_BUTTON,?self.bu_min,?self.button_44)

????????self.Bind(wx.EVT_BUTTON,?self.bu7,?self.button_46)

????????self.Bind(wx.EVT_BUTTON,?self.bu8,?self.button_45)

????????self.Bind(wx.EVT_BUTTON,?self.bu9,?self.button_47)

????????self.Bind(wx.EVT_BUTTON,?self.bu_mul,?self.button_48)

????????self.Bind(wx.EVT_BUTTON,?self.bu_clear,?self.button_49)

????????self.Bind(wx.EVT_BUTTON,?self.bu0,?self.button_50)

????????self.Bind(wx.EVT_BUTTON,?self.bu_result,?self.button_51)

????????self.Bind(wx.EVT_BUTTON,?self.bu_chu,?self.button_52)

????????#?end?wxGlade

????????self.Show(True)

????def?__set_properties(self):

????????#?begin?wxGlade:?MyFrame.__set_properties

????????self.SetTitle("Python?Calculater?by?CYG")

????????self.text_ctrl_1.SetMinSize((400,?50))

????????self.button_37.SetMinSize((100,?50))

????????self.button_38.SetMinSize((100,?50))

????????self.button_39.SetMinSize((100,?50))

????????self.button_40.SetMinSize((100,?50))

????????self.button_41.SetMinSize((100,?50))

????????self.button_42.SetMinSize((100,?50))

????????self.button_43.SetMinSize((100,?50))

????????self.button_44.SetMinSize((100,?50))

????????self.button_46.SetMinSize((100,?50))

????????self.button_45.SetMinSize((100,?50))

????????self.button_47.SetMinSize((100,?50))

????????self.button_48.SetMinSize((100,?50))

????????self.button_49.SetMinSize((100,?50))

????????self.button_50.SetMinSize((100,?50))

????????self.button_51.SetMinSize((100,?50))

????????self.button_52.SetMinSize((100,?50))

????????#?end?wxGlade

????def?__do_layout(self):

????????#?begin?wxGlade:?MyFrame.__do_layout

????????sizer_2?=?wx.BoxSizer(wx.VERTICAL)

????????sizer_3?=?wx.BoxSizer(wx.VERTICAL)

????????grid_sizer_1?=?wx.GridSizer(4,?4,?0,?0)

????????sizer_3.Add(self.text_ctrl_1,?0,?0,?0)

????????grid_sizer_1.Add(self.button_37,?0,?0,?0)

????????grid_sizer_1.Add(self.button_38,?0,?0,?0)

????????grid_sizer_1.Add(self.button_39,?0,?0,?0)

????????grid_sizer_1.Add(self.button_40,?0,?0,?0)

????????grid_sizer_1.Add(self.button_41,?0,?0,?0)

????????grid_sizer_1.Add(self.button_42,?0,?0,?0)

????????grid_sizer_1.Add(self.button_43,?0,?0,?0)

????????grid_sizer_1.Add(self.button_44,?0,?0,?0)

????????grid_sizer_1.Add(self.button_46,?0,?0,?0)

????????grid_sizer_1.Add(self.button_45,?0,?0,?0)

????????grid_sizer_1.Add(self.button_47,?0,?0,?0)

????????grid_sizer_1.Add(self.button_48,?0,?0,?0)

????????grid_sizer_1.Add(self.button_49,?0,?0,?0)

????????grid_sizer_1.Add(self.button_50,?0,?0,?0)

????????grid_sizer_1.Add(self.button_51,?0,?0,?0)

????????grid_sizer_1.Add(self.button_52,?0,?0,?0)

????????sizer_3.Add(grid_sizer_1,?1,?wx.EXPAND,?0)

????????sizer_2.Add(sizer_3,?1,?wx.EXPAND,?0)

????????self.SetSizer(sizer_2)

????????sizer_2.Fit(self)

????????self.Layout()

????????#?end?wxGlade

????def?bu1(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("1")

????????event.Skip()

????def?bu2(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("2")

????????event.Skip()

????def?bu3(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("3")

????????event.Skip()

????def?bu_plus(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.num1=self.text_ctrl_1.GetValue()

????????self.op="+"

????????self.text_ctrl_1.Clear()

????????event.Skip()

????def?bu4(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("4")

????????event.Skip()

????def?bu5(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("5")

????????event.Skip()

????def?bu6(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("6")

????????event.Skip()

????def?bu_min(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.num1=self.text_ctrl_1.GetValue()

????????self.op="-"

????????self.text_ctrl_1.Clear()

????????event.Skip()

????def?bu7(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("7")

????????event.Skip()

????def?bu8(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("8")

????????event.Skip()

????def?bu9(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("9")

????????event.Skip()

????def?bu_mul(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.num1=self.text_ctrl_1.GetValue()

????????self.op="x"

????????self.text_ctrl_1.Clear()

????????event.Skip()

????def?bu_clear(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.Clear()

????????event.Skip()

????def?bu0(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.text_ctrl_1.AppendText("0")

????????event.Skip()

????def?bu_chu(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.num1=self.text_ctrl_1.GetValue()

????????self.op="/"

????????self.text_ctrl_1.Clear()

????????event.Skip()

????def?bu_result(self,?event):??#?wxGlade:?MyFrame.event_handler

????????self.num2=self.text_ctrl_1.GetValue()

????????num1=int(self.num1)

????????num2=int(self.num2)

????????if?self.op=="+":

????????????self.text_ctrl_1.SetValue(str(num1+num2))

????????elif?self.op=="-":

????????????self.text_ctrl_1.SetValue(str(num1-num2))

????????elif?self.op=="x":

????????????self.text_ctrl_1.SetValue(str(num1*num2))

????????elif?self.op=="/":

????????????self.text_ctrl_1.SetValue(str(num1/num2))

????????event.Skip()

#?end?of?class?MyFrame

if?__name__?==?"__main__":

????app=wx.App(False)

????myframe=MyFrame(None)

????app.MainLoop()

(责任编辑:IT教学网)

更多

推荐其他WEB语言文章