python编写beta计算器_|python编写计算器

这篇博客介绍了如何使用Python编写一个简单的计算器程序,包括加减乘除运算。通过`raw_input`获取用户输入,根据操作符执行相应的计算。还讨论了代码中可能遇到的问题,如语法错误、使用`eval()`函数的风险,以及如何改进程序,如增加错误处理和选择运算类型的菜单功能。
摘要由CSDN通过智能技术生成

如何用python编辑计算器

def cal():

numA = float(raw_input('Please enter a number: '))

op = raw_input('Please enter a operator: ')

numB = float(raw_input('Please enter another number: '))

if op == ' ':

print "result: ",numA numB

elif op == '-':

print "result: ",numA-numB

elif op == '*':

print "result: ",numA*numB

elif op == '/':

print "result: ",numA/numB

else:

print "Unknown operator ",op

cal()

求用Python写一个简单的计算器!

没有错误判断,用eval一

def calc():

num1 = raw_input('Please enter a number: ')

op = raw_input('Please enter an operator: ')

num2 = raw_input('Please enter another number: ')

print '\nresult:', eval(num1 op num2)

if __name__ == '__main__':

calc()

python编写计算器

其实有个函数叫eval()的,至于怎么用,看你的了。

python编程 小小计算器

你使用了非ascii字符,就是第三行中的中文'符号',

把它改成英文就好了。

或者,你也可以在文件最前面,加上一行:

#

-*-

coding:

cp936

-*-

如何用python编程制作计算器

def cal():

numA = float(raw_input('Please enter a number: '))

op = raw_input('Please enter a operator: ')

numB = float(raw_input('Please enter another number: '))

if op == ' ':

print "result: ",numA numB

elif op == '-':

print "result: ",numA-numB

elif op == '*':

print "result: ",numA*numB

elif op == '/':

print "result: ",numA/numB

else:

print "Unknown operator ",op

cal()

如何用python编写计算器

# true needed a captial T

while True:

# Brackets were mismatched

CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION"))

if CHOICE == "1":

print 'ADDING TWO NUMBERS:'

# Calling a function shouldn't have trailing :

add(c)

elif CHOICE == "2":

print 'SUBTRACTING TWO NUMBERS'

# Calling a function shouldn't have trailing :

sub(c)

elif CHOICE == "3":

print 'MULTIPLYING TWO NUMBERS'

# Calling a function shouldn't have trailing :

Mul(c)

elif CHOICE == "4":

print "DIVIDEING TWO NUMBERS"

# Calling a function shouldn't have trailing :

Div(c)

elif CHOICE == "0":

# can only return from a function use exit here instead

exit()

# else needs a trailing :

else:

# No capital P for print

print "The value Enter value from 1-4"

The code now has no syntax errors but still has many problems.

You pass c to your function, c is never initialized, what is c?

Your function doesn't take arguments def add(): (even though pass the mysterious cvalue).

Your function doesn't print or return the result it just computes.

You store CHOICE as an int are do comparisons with strings so the else case is always executed and there is no way to exit the loop (infinite looping).

Fixed code:

#!/usr/bin/python

def add():

print "Enter the two numbers to Add"

A=int(raw_input("Enter A: "))

B=int(raw_input("Enter B: "))

return A B

def sub():

print "Enter the two numbers to Subtract"

A=int(raw_input("Enter A: "))

B=int(raw_input("Enter B: "))

return A - B

def mul():

print "Enter the two numbers to Multiply"

A=int(raw_input("Enter A: "))

B=int(raw_input("Enter B: "))

return A * B

def div():

print "Enter the two number to Divide"

A=float(raw_input("Enter A: "))

B=float(raw_input("Enter B: "))

return A / B

print "1: ADDITION"

print "2: SUBTRACTION"

print "3: MULTIPLICATION"

print "4: DIVITION"

print "0: QUIT"

while True:

CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION "))

if CHOICE == 1:

print 'ADDING TWO NUMBERS:'

print add()

elif CHOICE == 2:

print 'SUBTRACTING TWO NUMBERS'

print sub()

elif CHOICE == 3:

print 'MULTIPLYING TWO NUMBERS'

print mul()

elif CHOICE == 4:

print "DIVIDEING TWO NUMBERS"

print div()

elif CHOICE == 0:

exit()

else:

print "The value Enter value from 1-4"

python写了一段计算器代码,怎么让它在winxp下运行啊!谢谢!

简单说就是要安装个python的解释器

具体做法 请搜所一本叫的书,有在线版

版权声明:本站所有文章皆为原创,欢迎转载或转发,请保留网站地址和作者信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值