-3

Why am I getting an error with map() function in the following code? Why can't map convert negative numbers in the list x. The output should be like "1 --> 2 --> 3". The list should end whenever I enter -999. I am getting an error such as:

Traceback (most recent call last):
  File "c2.py", line 3, in <module>
    x=map(int,x)
ValueError: invalid literal for int() with base 10: '-'

Code:

while(1):
    x=list(raw_input("Input a number\n(type -999 to end);"))
    x=map(int,x)
    if x<0:
        break
    pass
    print x
del x[len(x)]
for i in range(0,(len(x))):
    print "%d-->" %(x[i]),
Will Ness
  • 70,110
  • 9
  • 98
  • 181
proggeek
  • 21
  • 4
  • `int(x)` will indeed convert `x` to an integer, no `map` required; but not if `x` is a list. So you should also remove the `list` call on the second line. – Kevin Sep 03 '14 at 12:52
  • 1
    What format should the input take? `"1 2 3 4 -999"` on a single line, or separate inputs? Note that `list("-999") == ['-', '9', '9', '9']`. – jonrsharpe Sep 03 '14 at 12:52
  • Since `raw_input` returns a string, `list(raw_input(...))` will break that string into a list containing the characters of the original string. – chepner Sep 03 '14 at 12:58
  • `pass` is only needed if it is the *only* statement in a block. – chepner Sep 03 '14 at 12:59

3 Answers3

1

thank you ... got it :)

    x=[]
    while(1):
       s=raw_input("Input a number\n(type -999 to end);")
       s=int(s)
       x.append(s)
       if s<0:
          break
       pass
    print "\n%d" %(x[0]),
    for i in range(1,(len(x))):
        print "-->%d" %(x[i]),
    print "\n\nNumber of items = %d" %(len(x)-1)
proggeek
  • 21
  • 4
  • Don't answer yourself, in the future, edit something like this into the original post under a bar or something. Otherwise folks will continue to answer your question. – Jon Jay Obermark Sep 03 '14 at 13:13
  • @JonJayObermark see http://meta.stackexchange.com/a/216722/248731 - answers should be answers, and if other users think can improve further on the OP's they are welcome to try. – jonrsharpe Sep 03 '14 at 13:16
  • Sorry. That is bizarre etiquette, but I guess it is the local rule. (I do see what I suggest done here all the time, and I, personally, appreciate it.) If this is the answer, at the very least the question as stated needs to be edited, as the answer has nothing to do with the actual questions in the OP, or the title. – Jon Jay Obermark Sep 03 '14 at 15:08
0

Try

x = map(int,raw_input().split())
Inox
  • 2,255
  • 3
  • 13
  • 26
  • Ok, now x will be a list of integers, which at least gets rid of the OP's `ValueError`. But then he'll get into an infinite loop because `x < 0` will always evaluate to `False`. Or he'll get `TypeError: unorderable types: list() < int()` if he tries in 3.x – Kevin Sep 03 '14 at 12:59
0

Not addressing the other bugs unrelated to map, the problem is that map returns a list.

Check it out in IDLE:

>>> map(int, ["123", "456", 7.3])
[123, 456, 7]
>>>

So x<0 makes no sense, you would want something like min(x) < 0. Something that takes a list as input.

This is a case where you should probably isolate and process each entry individually. Get the input line, scan through it for spaces (or split() it, as noted below) and parse each integer.

Even if you used split and map correctly, you are automatically going to process the integers on the same line with the -999, but after it. And anything that does not parse will not give you enough feedback to know what really went wrong.

Parsing user input often has to be low-level and character-oriented, or it cannot diagnose their incorrect entry. Sometimes life is tedious.

(Also, why empty the list and then try to print it?)