Issue
I am a iPython beginner and I need some help with my code. I want to read 6 values from input. They can be integers, floats, complex numbers, strings or lists. After reading the values and appending them to a list, I do the following conversion:
for integers: replace it with the square root inside the list.
for floats: replace it with the cosine of the number inside the list.
for complex numbers: replace it with its real part inside the list.
for strings: turn it to capital letters inside the list.
for lists: discard the last element.
The program throws the following error:
File "<ipython-input-1-1f00b88d82c9>", line 20
else i is list:
^
SyntaxError: invalid syntax
and also it can not store a string value into list. It always stores . how should I solve this error? Here is my code:
import math as m
l=[]
i=0
while i<6:
x=input("Enter some values: ")
if isinstance(x,str):
x=int(x)
l.append(x)
i=i+1
print l
for i in l:
if i is str:
i=s.capitalize(i)
elif i is int:
i=m.sqrt(i)
elif i is float:
i=m.cos(i)
elif i is complex:
i=l.real(i)
else i is list:
n=len(l)
i=remove[n-1]
print l
Solution
Just replace
else i is list:
with
elif i is list:
Since you need to evaluate a condition even in your last case, you need the 'if' functionality, so use elif
. else
cannot be followed immediately by a condition, hence the error you saw.
Also, you have error in line
i=remove[n-1]
If you want remove last element from list i
you need
del i[-1]
Answered By - kvorobiev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.