首页 > python中的raw_put接收问题,试了好久都不行,求大神解答~

python中的raw_put接收问题,试了好久都不行,求大神解答~

#!/usr/bin/env python
# -*- coding: utf-8 -*-
args = raw_input('Give your argv:')
script, first, second, third = args
print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

运行后不管我输入用逗号隔开的字符串还是数组都提示我错误:

Give your argv:['safd','wer','231','hgh']
Traceback (most recent call last):
  File "/Users/JS000/Desktop/first try/try.py", line 5, in <module>
    script, first, second, third = args
ValueError: too many values to unpack
Process finished with exit code 1

请问输入什么才能正常运行?


args = raw_input('Give your argv:')
接收到的输入值都会转化成字符串对象,你输入了四个项,那么比方是 'a b c d'
然后在Python里你用了序列赋值语句
script, first, second, third = args
序列赋值语句右侧可以是任何类型的序列(list, tuple, str),但是长度必须与左边的目标数相等。
现在程序里的args是个长度为7的字符串所以会出现上面提到的那个错误。
可以用修改下程序:

args = raw_input('Give your argv:')
arg_list = args.split(' ')
script, first, second, third = arg_list
......

raw_inpout返回的是字符串类型,并不是list/tuple.
script, first, second, third = args 不是都已经抛异常了么.

试试input函数吧

【热门文章】
【热门文章】