Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Abraxas

  • 3 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
  1. snark
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 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. andrew.m.higgs
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yes. As Snark says. You need to add the converted strings to a list to be returned.

  3. Abraxas
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Nice Snark!!! Thank a lot!!!! Take your medal. You deserve a lot of it!

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy