python计算器程序_python计算器程序

1586010002-jmsa.png

I wrote a simple calculator program by using functions, I don't know what exactly wrong with this code, its showing error. I did possible steps to debug this, but I couldn't.

#!/usr/bin/python

def add():

print "Enter the two numbers to Add"

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

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

c = A + B

def sub():

print "Enter the two numbers to Subtract"

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

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

c = A - B

def Mul():

print "Enter the two numbers to Multiply"

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

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

c = A * B

def Div():

print "Enter the two number to Divide"

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

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

c = 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:'

add(c):

elif CHOICE == "2":

print 'SUBTRACTING TWO NUMBERS:'

sub(c):

elif CHOICE == "3":

print 'MULTIPLYING TWO NUMBERS:'

Mul(c):

elif CHOICE == "4":

print "DIVIDEING TWO NUMBERS"

Div(c):

elif CHOICE == "0":

return 0:

else

Print "The value Enter value from 1-4"

Error:

File "cal_fun.py", line 44

if CHOICE == "1":

^

SyntaxError: invalid syntax

解决方案

I've have tried to cover all of the problems with your code, of which there are numerous.

Starting with syntax errors:

# 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 c value).

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"

The code is now functional.

Output:

1: ADDITION

2: SUBTRACTION

3: MULTIPLICATION

4: DIVITION

0: QUIT

ENTER THE CORRESPONDING NUMBER FOR CALCULATION 1

ADDING TWO NUMBERS:

Enter the two numbers to Add

Enter A: 2

Enter B: 5

7

ENTER THE CORRESPONDING NUMBER FOR CALCULATION 2

SUBTRACTING TWO NUMBERS

Enter the two numbers to Subtract

Enter A: 2

Enter B: 5

-3

ENTER THE CORRESPONDING NUMBER FOR CALCULATION 3

MULTIPLYING TWO NUMBERS

Enter the two numbers to Multiply

Enter A: 2

Enter B: 5

10

ENTER THE CORRESPONDING NUMBER FOR CALCULATION 4

DIVIDEING TWO NUMBERS

Enter the two number to Divide

Enter A: 2

Enter B: 5

0.4

ENTER THE CORRESPONDING NUMBER FOR CALCULATION 0

Functional but not perfect, for instance no error handling for erroneous input.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值