-3

次のコードの map() 関数でエラーが発生するのはなぜですか? リスト内の負の数をマップ変換できないのはなぜですかx。出力は「1 --> 2 --> 3」のようになります。と入力するたびに、リストは終了するはず-999です。次のようなエラーが表示されます。

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

コード:

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]),
4

3 に答える 3

1

ありがとう...わかりました:)

    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)
于 2014-09-03T13:01:39.070 に答える