如何用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的解释器
具体做法 请搜所一本叫的书,有在线版
版权声明:本站所有文章皆为原创,欢迎转载或转发,请保留网站地址和作者信息。