有人可以为我检查这个请非常新的python

时间:2015-09-10 20:18:19

标签: python python-3.x helpers

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)

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

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

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

4 个答案:

答案 0 :(得分:3)

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'''

其余的似乎没问题。

答案 1 :(得分:0)

如果你想要一个像这样的多行字符串,你应该使用三个引号来预先形成只有\ 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"""

答案 2 :(得分:0)

您已在此行的末尾留下额外的\

9. Square root the second number\n\"

这会转义"并导致字符串无法终止。

最好在多线字符串周围使用三引号,如@hiro建议

答案 3 :(得分:0)

除了多行字符串外,以'''代替'',一切都好。现在你应该尝试减少代码行数。您可以在优化工作脚本时学习。