2015-09-10 2 views
-6
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)) 
+0

également dans mon code les tirages sont commandés correctement –

+0

En regardant la coloration du texte faite par le formateur de code, il semble que vous ayez une constante chaîne non terminée quelque part. Vous avez besoin d'une citation supplémentaire! – Kevin

Répondre

3

les chaînes multilignes en python sont délimitées par '''.

vous devez écrire:

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

le reste semble ok.

0

Vous devez utiliser trois guillemets si vous souhaitez avoir une chaîne multiligne comme celle-là aussi pour préformer un saut de ligne qui est juste \ n. J'espère que j'ai aidé.

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""" 
0

Vous avez laissé un \ supplémentaire à la fin de cette ligne:

9. Square root the second number\n\" 

Ce échappe à l'" et provoque la chaîne de ne pas être terminée.

Il est préférable d'utiliser des triples guillemets autour de la chaîne multiligne comme @hiro suggère

0

Sauf la chaîne multiligne, a commencé avec « » » à la place de « », tout est ok. Maintenant, vous devriez essayer d'obtenir moins de lignes de code. Vous apprenez lorsque vous optimisez vos scripts de travail.