• Register
1 vote
26 views

Problem:

I am beginner in pyhton prgramming language, I tried to convert string array to int array using python so I wrote a code which contains-
testing_list = ['4', '2', '3', '9', '5'] 

And my code is:

testing_list = ['4', '2', '3', '9', '5'] 

print ("Main list is : " + str(testing_list)) 

for i in range(0, len(testing_list)): 
    testing_list[i] = int(testing_list[i]) 
    print ("Converted list is : " + 'str(testing_list)') 

And my codes output is-

Main list is : ['4', '2', '3', '9', '5']
Converted list is : str(testing_list)
Converted list is : str(testing_list)
Converted list is : str(testing_list)
Converted list is : str(testing_list)
Converted list is : str(testing_list)

why am I not getting the converted result and what is the way to get the acurate result?

10 7 2
6,050 points

1 Answer

1 vote

Answer:

You should not put your string in a qoute. You should have written the code as-
testing_list = ['4', '2', '3', '9', '5'] 

print ("Main list is : " + str(testing_list)) 

for i in range(0, len(testing_list)): 
    testing_list[i] = int(testing_list[i]) 
   
    print ("Converted list is : " + str(testing_list)) 

And the output is:

Main list is : ['4', '2', '3', '9', '5']
Converted list is : [4, '2', '3', '9', '5']
Converted list is : [4, 2, '3', '9', '5']
Converted list is : [4, 2, 3, '9', '5']
Converted list is : [4, 2, 3, 9, '5']
Converted list is : [4, 2, 3, 9, 5]

Your problem was in the line-

 print ("Converted list is : " + 'str(testing_list)') 

 

11 6 4
34,930 points

Related questions

1 vote
1 answer 15 views
15 views
Problem: I was tring to find the length of a string in python. So I wrote a code: string = 0 for c in "popular": string+==1 print (string) which gives following error; File "ab.py", line 3 string+==1 ^ SyntaxError: invalid syntax Is this the right way to do this or there are other waysto find this out?
asked Mar 28 LizzyM 6.1k points
1 vote
1 answer 315 views
315 views
Problem: I am getting the following error : error: array initializer must be an initializer list or string literal For the array; char x[80] = GetString(); Can any body explain or help?
asked Apr 2 LizzyM 6.1k points
1 vote
1 answer 38 views
38 views
Problem: I am a python newbee. I was trying to print string and int on same line of a python code. The code I wrote is: a = int(input("Please Enter a Number: ")) print("Your Number is" a) but when I run this it shows an error message: File "main.py", line 2 print("Your Number is" x) ^ SyntaxError: invalid syntax I am not able to do this please help me to solve this problem?
asked Mar 27 LizzyM 6.1k points
0 votes
1 answer 5 views
5 views asked 4 days ago SapperIt 1.2k points