【问题标题】:Python Code not working correctlyPython 代码无法正常工作
【发布时间】:2014-03-23 02:18:14
【问题描述】:

您好,这是我在此代码上的第二篇文章,我没有收到错误,但 if else 语句无法按照我想要的方式运行 if(calculate[2] == 操作: 代码不起作用,即使我有 - 或 + 也不会注册。任何想法为什么?它给了我回复Sorry invalid text3。

calculate = input("Enter the problem in the format x + y = z ")

opperations = ["+", "-", "*", "/"]
numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
space = " "
a = calculate[0]
a = int(a)
b = calculate[4]
b = int(b)

def opperation():
if opperations == "+":
    A = 1
elif opperations == "-":
    A = 2
elif opperations == "*":
    A = 3
elif opperations == "/":
    A = 4

if calculate[0] in numbers:
    if len(calculate) > 1:
        if calculate[2] == opperations:
            if calculate[1] == space:
                if A == 1:
                    c = a + b
                    print (c)
                elif A == 2:
                    c = a - b
                    print (c)
                elif A == 3:
                    c = a * b
                    print(c)
                elif A == 4:
                    c = a / b
                    print(c)
                else:
                    print("Sorry invalid text5")
            else:
                print("Sorry invalid text4")
        else:
            print("Sorry invalid text3")
    else:
        print("Sorry invalid text2")
else:
    print("Sorry invalid text1") 

【问题讨论】:

  • 我猜你想使用 calculate[2] in opperations 而不是你现在拥有的。
  • 除非您特别询问如何解决跨版本兼容性问题(在这种情况下,您的问题显然应该描述该问题),否则您不应混合使用python-2.7python-3.x 标签。我暂时将它们都删除了。

标签: python


【解决方案1】:

条件

if calculate[2] == opperations:

会将calculate[2] 与整个列表["+", "-", "*", "/"] 进行比较。您可以改为使用 in 关键字来检查元素是否在序列中:

if calculate[2] in opperations:
    # ...

另外,在将opperations"+""- 进行比较的部分...您应该将它们与calculate[2] 进行比较:

if calculate[2] == "+":
    A = 1
elif calculate[2] == "-":
    A = 2
elif calculate[2] == "*":
    A = 3
elif calculate[2] == "/":
    A = 4

注意:您的方法将只处理带有1 数字的操作。我建议您使用 split(" ") 方法,以便您可以像这样使用它:

s = "100 + 20"
parts = s.split(" ")   # split by space
parts[0] # 100
parts[1] # +
parts[2] # 20

【讨论】:

    【解决方案2】:

    除了 Christian 的建议之外,ifelif 链可以被字典查找替换。在此示例中,lambda a, b: a+b 是一个函数,它接受两个值 ab 并返回总和。这意味着opperations["+"] 返回一个函数,opperations["+"](a,b) 返回ab 的总和。

    opperations = { "+": lambda a, b: a+b, 
                   "-": lambda a, b: a-b,
                   "*": lambda a, b: a*b,
                   "/": lambda a, b: a/b }
    
    calculate = raw_input("Enter the problem in the format x + y = z ")
    
    numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    space = " "
    a = calculate[0]
    a = int(a)
    b = calculate[4]
    b = int(b)
    
    if calculate[0] in numbers:
        if len(calculate) > 1:
            if calculate[2] in opperations:
                if calculate[1] == space:
                    c = opperations[calculate[2]](a,b)
                    print (c)
                else:
                    print("Sorry invalid text4")
            else:
                print("Sorry invalid text3")
        else:
            print("Sorry invalid text2")
    else:
        print("Sorry invalid text1") 
    

    【讨论】:

      【解决方案3】:

      我认为创建一个新的python文件然后在其中创建一个函数然后导入它会更容易:

      def calculate(calculation):
          with open('calculation_file', 'w') as FILE:
              FILE.write(
              '''
              def calculate():
                  return ''' + calculation + '''
              ''')
          import calculation_file as c
          return c.calculate
      

      它创建一个文件,然后导入它并返回计算文件 There are probably some bugs though because I have not run it yet 您也可能需要将变量 abint(a) 一起设为整数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-28
        • 2017-08-01
        • 1970-01-01
        • 2015-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        相关资源
        最近更新 更多