Quantcast

A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Abraxas

  • 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
  1. snark
    • 2 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
    • 2 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
    • 2 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

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.