A community for students.
Here's the question you clicked on:
 0 viewing
 2 years ago
Who could help me to understand the behavior of Python in this function?
I'm trying to convert into integers some numeric strings inside a list.
Numbers = ('1', '2', '3', '4')
def StrToInt(x):
for i in x:
print i
int(i)
I think it doesn't work because function assign a value to a local variable. But, if a try something like:
def StrToInt(x):
for i in x:
print i
int(i)
return x
or
def StrToInt(x):
global x
for i in x:
print i
int(i)
both functions doesn't work either.
 2 years ago
Who could help me to understand the behavior of Python in this function? I'm trying to convert into integers some numeric strings inside a list. Numbers = ('1', '2', '3', '4') def StrToInt(x): for i in x: print i int(i) I think it doesn't work because function assign a value to a local variable. But, if a try something like: def StrToInt(x): for i in x: print i int(i) return x or def StrToInt(x): global x for i in x: print i int(i) both functions doesn't work either.

This Question is Closed

snark
 2 years ago
Best ResponseYou've already chosen the best response.3numbers = ['1' ,'2' ,'3'] # our list of numeric strings def toInt(x): # a function which will use x, here a list of strings a = [ ] # create an empty list to hold our integers for n in x: # for each entry in the list x a.append(int(n)) # put the int value of n into our list return a # return our list of integers b = toInt(numbers) # let's test it print(b) [1,2,3] # Surprise!

andrew.m.higgs
 2 years ago
Best ResponseYou've already chosen the best response.0Yes. As Snark says. You need to add the converted strings to a list to be returned.

Abraxas
 2 years ago
Best ResponseYou've already chosen the best response.0Nice Snark!!! Thank a lot!!!! Take your medal. You deserve a lot of it!
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.