【问题标题】:syntax error in the code in the line break list due to the input, how do you adjust this? [closed]由于输入导致换行列表中的代码出现语法错误,如何调整? [关闭]
【发布时间】:2015-12-07 06:34:39
【问题描述】:

输入和列表之间的多行代码也存在语法错误。


    Import math 
    o = " What operation do you want to use :\n\

    1. Add\n\ 
    2. Subtract\n\
    3. Multiply\n\
    4. Divide\n\
    5. Square the first number\n\
    6. Square the second number\n\
    7. Calculate the power of the first number to the second number\n\
    8. Square root the first number\n\
    9. Square root the second number\n\"

    n1 = int(input(" Enter the first number: "))

    n2 = int(input(" Enter the second number: "))

    print(o) 

    oc = int(input("Enter the operation you have chose:"))

    if oc == 1: 
     print(n1 + n2)

    elif oc == 2:
        print (n1 - n2)

    elif oc == 3:
        print(n1 * n2)

    elif oc == 4:
        print(n1 / n2)

    elif oc == 5:
        print(n1**2)

    elif oc == 6:
        print(n2*2)
```this does not work properly```
    elif oc == 7:
        print(n1**n2)

    elif oc == 8:
        print(math.sqrt(n1))

    elif oc == 9:
        print(math.sqrt(n2))

代码没有正确处理输入,使用数组或列表会更容易。甚至使用 numpy 并使用文本文件。怎么会

【问题讨论】:

  • 在我的代码中,打印的顺序也是正确的
  • 只看代码格式化程序完成的文本着色,看起来你在某处有一个未终止的字符串常量。你需要一个额外的引号!

标签: python python-3.x helper


【解决方案1】:

python中的多行字符串由'''分隔。

你应该写:

o = ''' What operation do you want to use :

1. Add
2. Subtract
3. Multiply
4. Divide
5. Square the first number
6. Square the second number
7. Calculate the power of the first number to the second number
8. Square root the first number
9. Square root the second number'''

其余的似乎还可以。

【讨论】:

    【解决方案2】:

    你在这一行的末尾多留了一个\

    9. Square root the second number\n\"
    

    这会转义" 并导致字符串不被终止。

    按照@hiro 的建议,最好在多行字符串周围使用三引号

    【讨论】:

      【解决方案3】:

      除了多行字符串,以'''代替''开头,一切正常。现在您应该尝试减少代码行数。当你优化你的工作脚本时,你会学到。

      【讨论】:

        【解决方案4】:

        如果您要使用这样的多行字符串也应该使用三个引号来执行只是 \n 的换行符。希望我能帮上忙。

        import math 
        o = """ What operation do you want to use :\n
        
        1. Add\n
        2. Subtract\n
        3. Multiply\n
        4. Divide\n
        5. Square the first number\n
        6. Square the second number\n
        7. Calculate the power of the first number to the second number\n
        8. Square root the first number\n
        9. Square root the second number\n"""
        

        【讨论】:

          猜你喜欢
          • 2013-04-30
          • 1970-01-01
          • 2015-06-04
          • 2015-01-25
          • 2018-09-28
          • 2011-09-10
          • 2013-05-02
          • 2014-07-11
          • 2022-11-08
          相关资源
          最近更新 更多