I am out of trial and error again Lists

Seymore4Head Seymore4Head at Hotmail.invalid
Fri Oct 24 16:00:30 EDT 2014


On Fri, 24 Oct 2014 12:55:19 -0700 (PDT), sohcahtoa82 at gmail.com wrote:

>On Friday, October 24, 2014 12:36:23 PM UTC-7, Seymore4Head wrote:
>> On Fri, 24 Oct 2014 12:25:33 -0700 (PDT), sohcahtoa82 at gmail.com wrote:
>> 
>> >On Friday, October 24, 2014 12:12:10 PM UTC-7, Seymore4Head wrote:
>> >> On Fri, 24 Oct 2014 11:57:12 -0700 (PDT), sohcahtoa82 at gmail.com wrote:
>> >> 
>> >> >On Friday, October 24, 2014 11:17:53 AM UTC-7, Seymore4Head wrote:
>> >> >> On Fri, 24 Oct 2014 11:52:15 -0600, Ian Kelly <ian.g.kelly at gmail.com>
>> >> >> wrote:
>> >> >> 
>> >> >> >On Fri, Oct 24, 2014 at 11:03 AM, Seymore4Head
>> >> >> ><Seymore4Head at hotmail.invalid> wrote:
>> >> >> >> Actually I was a little frustrated when I added that line back in as
>> >> >> >> the other lines all work.
>> >> >> >> Using list(range(10)) Doesn't throw an error but it doesn't work.
>> >> >> >>
>> >> >> >> http://i.imgur.com/DTc5zoL.jpg
>> >> >> >>
>> >> >> >> The interpreter.   I don't know how to use that either.
>> >> >> >
>> >> >> >Try both of these in the interpreter, and observe the difference:
>> >> >> >
>> >> >> >7 in range(10)
>> >> >> >
>> >> >> >"7" in range(10)
>> >> >> >
>> >> >> >Do you understand what the difference between 7 and "7" is?
>> >> >> 
>> >> >> I do understand that.  7 is a number and "7" is a string.
>> >> >> What my question was...and still is...is why
>> >> >> Python 3 fails when I try using
>> >> >> y=1 800 get charter
>> >> >> 
>> >> >> y in range str(range(10))
>> >> >> should work because y is a string and str(range(10)) should be
>> >> >> "y" in str(1) fails.
>> >> >> It doesn't give an error it's just not True when y is a number.
>> >> >> 
>> >> >> These hints are just not working.  I am too thick for hints. :)
>> >> >> If you could use it in the code, I might understand.
>> >> >> The other work arounds that were posted work.
>> >> >> I have used them.  str(range(10)) doesn't work.
>> >> >> 
>> >> >> import string
>> >> >> def nametonumber(name):
>> >> >>     lst=[]
>> >> >>     nx=[]
>> >> >>     digit=[]
>> >> >>     digit="".join(str(i) for i in range(10))
>> >> >>     for x in name:
>> >> >>         lst.append(x)
>> >> >>     for y in (lst):
>> >> >>         if y in list(range(1,10)):
>> >> >>         #if y in "1234567890":
>> >> >>         #if y.isdigit():
>> >> >>         #if y in digit:       
>> >> >>         #if y in string.digits:
>> >> >>             nx.append(y)
>> >> >>         if y in " -()":
>> >> >>             nx.append(y)
>> >> >>         if y in "abc":
>> >> >>             nx.append("2")
>> >> >>         if y in "def":
>> >> >>             nx.append("3")
>> >> >>         if y in "ghi":
>> >> >>             nx.append("4")
>> >> >>         if y in "jkl":
>> >> >>             nx.append("5")
>> >> >>         if y in "mno":
>> >> >>             nx.append("6")
>> >> >>         if y in "pqrs":
>> >> >>             nx.append("7")
>> >> >>         if y in "tuv":
>> >> >>             nx.append("8")
>> >> >>         if y in "wxyz":
>> >> >>             nx.append("9")
>> >> >>     number="".join(e for e in nx)
>> >> >>     return number
>> >> >> a="1-800-getcharter"
>> >> >> print (nametonumber(a))#1800 438 2427 837
>> >> >> a="1-800-leo laporte"
>> >> >> print (nametonumber(a))
>> >> >> a="1 800 dialaho"
>> >> >> print (nametonumber(a))
>> >> >> 
>> >> >> Please
>> >> >
>> >> >Your code here is actually pretty close to a correct answer.  Just a few things to consider...
>> >> >
>> >> >- Why are you converting your name string to a list?  It is unnecessary.  When you do "for y in <some string>", then y will still be single characters on each iteration of the loop.
>> >> >
>> >> >- "if y in string.digits" should work fine.
>> >> >
>> >> >- "if y in list(range(1,10)" won't work for two reasons: First, it creates a list of numbers, not strings.  Second, even if it did, it would be missing the "0" digit.
>> >> >
>> >> >- At the end, when you convert your list to a string, you don't need to use list comprehension, since nx is already a list.  number = "".join(nx) should work fine.
>> >> >
>> >> >Also, in general, you need to stop and slow down and think like a programmer.  If you get an error, your instinct shouldn't be to just hack at it to make the error go away.  Look at the error and try to make sense of it.  Learn what the error means and try to fix the core problem.
>> >> >
>> >> >And for @#$%'s sake...stop saying "It isn't working" and not elaborating.  You've been told by every other post in this thread to show us what you did and what the error was.  You've also been told to *NOT* retype what you see and to copy/paste your code and the error because when you make a typo when copying, we might see a problem that doesn't exist and then you just get more confused.
>> >> 
>> >> Ok  I think I may have the question you guys are looking for.
>> >> I just posted it.
>> >> See above.
>> >> 
>> >> But it's still broke.  :(
>> >
>> >str(range(10)) doesn't do what you think it does.
>> >
>> >Run 'print(str(range(10)))' and look at what you get.
>> 
>> Yeah, I know that.  My question is why?
>> The answer was that Python 3 only stores the min and max values but
>> you can still iterate over them.
>> I don't think that means what I think it means.
>
>"You can iterate over them" pretty much just means you can use them as the source of a 'for' loop.  But in your case, when you're calling 'for y in str(range(10))', you're not using 'range(10)' as the source of your loop, you're using the result of a str() function call, and you're calling str() on range(), which doesn't return a concrete value in Python 3.  If you try to print a range(), you're just getting a string containing your original call to range.
>
>And that's why you're seeing the 1 and a in your output.  str(range(10)) returns the string 'range(10)'.
>
>Like I said in a previous post, use 'string.digits'.  Try this test code and see what you get:
>
>import string
>name="123-xyz-abc" 
>print("string.digits is", string.digits)
>for x in name: 
>    if x in range(10): 
>        print ("Range",(x)) 
>    if x in string.digits: 
>        print ("string.digits",(x)) 

Your example works.
With range(10) you don't expect to see an "a" either.
Thanks



More information about the Python-list mailing list