计算器程序中的语法错误

时间:2015-10-13 21:19:45

标签: python syntax-error python-2.x python-2.4

我把这段代码写成计算器,但是我收到了语法错误,因为我需要定义[add,subtract等]我该怎么做?

from math import*

def main():
print("Tremelling Enterprises: Calculator s1-1SC  ")
raw_input('What would you like to do?  add, subtract, divide, multiply,         powers, or square roots?')
if == add:
        print("Welcome to the adding department! ")
        print("What are your two values?")
        x = raw_input('What is the value of the first number? ')
        y = raw_input('What is the value of the second number? ')
        solution1 = x + y
        print(solution1)
    if == subtract:
        print("Welcome to the subtracting department! ")
        print("What are your two values?")
        a = raw_input('What is the value of the first number? ')
        b = raw_input('What is the value of the second number? ')
        solution2 = a - b
        print(solution2)
    if == divide:
        print("Welcome to the division department! ")
        print("What are your two values?")
        c = raw_input('What is the value of the first number? ')
        d = raw_input('What is the value of the second number? ')
        solution3 = c / d
        print(solution3)        
    if == multiply:
        print("Welcome to the multiplication department! ")
        print("What are your two values?")
        e = raw_input('What is the value of the first number? ')
        f = raw_input('What is the value of the second number? ')
        solution4 = e * f
        print(solution1)
    if == powers:
        print("Welcome to the power department! ")
        print("What are your two values?")
        g = raw_input('What is the value of the base? ')
        h = raw_input('What is the value of the exponent? ')
        solution5 = pow(g,h)
        print(solution5)
    if == square roots:
        print("Welcome to the square roots department!")
        print("what is your value?")
        i = raw_input('number')
        solution6 = sqrt(i)
        print(solution6)
    else:
        print("Sorry, that function is not availible at this time.")
main()

如果这可以运行并且正常运行将会很棒。谢谢

P.S。这不适合课程,但只是一个有趣的课程不适用于任何学分。

1 个答案:

答案 0 :(得分:0)

我会给你一些提示。你走在正确的轨道上。您有两种不同的方法来处理raw_input,其中一种方法是正确的。所以这里是前几行的一些变化,它们可以让你在短时间内完成其余的工作。

def main():
  print("Tremelling Enterprises: Calculator s1-1SC  ")
  action = raw_input('What would you like to do?  add, subtract, divide, multiply,         powers, or square roots?')
  if action == "add":
        print("Welcome to the adding department! ")
        print("What are your two values?")
        x = raw_input('What is the value of the first number? ')
        y = raw_input('What is the value of the second number? ')
        solution1 = x + y
        print(solution1)