Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
Abraxas
Group Title
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
 2 years ago
Abraxas Group Title
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
 2 years ago

This Question is Closed

snark Group TitleBest ResponseYou've already chosen the best response.3
numbers = ['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!
 2 years ago

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

Abraxas Group TitleBest ResponseYou've already chosen the best response.0
Nice Snark!!! Thank a lot!!!! Take your medal. You deserve a lot of it!
 2 years ago
See more questions >>>
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.