#!/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"
错误:
File "cal_fun.py", line 44
if CHOICE == "1":
^
SyntaxError: invalid syntax
答案 0 :(得分:4)
我已尝试用您的代码解决所有问题,其中有很多问题。
从syntax
错误开始:
# 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"
代码现在没有syntax
错误,但仍有许多问题。
c
传递给您的函数,c
永远不会被初始化,什么是c
?def add():
(即使传递了神秘的c
值)。 print
或return
它只计算结果。CHOICE
作为int
与字符串进行比较,因此始终执行else
大小写并且无法退出循环(无限循环)< / em>的。 固定代码:
#!/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"
代码现在正常运作。
<强>输出:强>
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
功能但不完美,例如对错误输入没有错误处理。
答案 1 :(得分:1)
你错过了前一行的结束括号(神秘语法错误的常见原因),更改:
CHOICE = int(raw_input(("ENTER THE CORRESPONDING NUMBER FOR CALCULATION"))
到
CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION"))
这不是程序中唯一的语法错误 - 当你不应该使用:
结束许多行时,例如:
add(c):
sub(c):
Mul(c):
Div(c):
你也
:
语句没有else
(需要)Print
应该是print
还有一些错误不是语法错误:
True
true
CHOICE
(一个int)与"1"
或"2"
c
传递给不带参数的函数答案 2 :(得分:1)
您正在将变量c
传递给函数add()
sub()
等,但它们被定义为不带参数。
答案 3 :(得分:1)
在已经提到的语法错误的基础上我认为你真正想要的是每个函数将值返回到主程序循环,然后显示它们:
def add():
A=int(raw_input("Enter A:"))
B=int(raw_input("ENter B:"))
return A + B
...
while true:
CHOICE = int(raw_input(("ENTER THE CORRESPONDING NUMBER FOR CALCULATION"))
if CHOICE == "1":
print 'ADDING TWO NUMBERS:'
answer = add()
print answer
...
或者通过在主循环中输入A和B然后将它们作为参数传递给计算函数来缩短程序:
def add():
return A + B
...
while true:
CHOICE = int(raw_input(("ENTER THE CORRESPONDING NUMBER FOR CALCULATION"))
A=int(raw_input("Enter A:"))
B=int(raw_input("ENter B:"))
if CHOICE == "1":
print 'ADDING TWO NUMBERS:'
answer = add(A, B)
print answer
...