没有足够的值在Python中解包
我试图允许用户在Python中操作列表没有足够的值在Python中解包,python,python-3.x,Python,Python 3.x,我试图允许用户在Python中操作列表 number_of_commands = int(input()) x = 0 my_list = [] while x <= number_of_commands: command, i, e = input().split(' ') command = str(command) i = int(i) e = int(e) x = x + 1 if command == 'insert':
number_of_commands = int(input())
x = 0
my_list = []
while x <= number_of_commands:
command, i, e = input().split(' ')
command = str(command)
i = int(i)
e = int(e)
x = x + 1
if command == 'insert':
my_list.insert(i, e)
elif command == 'print':
print(my_list)
elif command == 'remove':
my_list.remove(e)
elif command == 'append':
my_list.append(e)
elif command == 'sort':
my_list.sort()
elif command == 'pop':
my_list.pop()
elif command == 'reverse':
my_list.reverse()
else:
print("goodbye")
numberofu命令=int(input())
x=0
我的清单=[]
而x这里是解包的地方:
command, i, e = input().split(' ')
仅输入“print”将不允许该行正确执行,因为未提供i和e的值
因此,只需读取输入,然后拆分它并检查用户提供了多少参数:
input_str = input()
input_str_split = input_str.split(' ')
if len(input_str_split) == 3:
command, i, e = input_str_split
i = int(i)
e = int(e)
else:
command = input_str_split
这里是解包的地方:
command, i, e = input().split(' ')
仅输入“print”将不允许该行正确执行,因为未提供i和e的值
因此,只需读取输入,然后拆分它并检查用户提供了多少参数:
input_str = input()
input_str_split = input_str.split(' ')
if len(input_str_split) == 3:
command, i, e = input_str_split
i = int(i)
e = int(e)
else:
command = input_str_split
你可以用
command, *values = input().split(' ')
值是一个列表。例如,“插入”零件变为:
if command == 'insert':
my_list.insert(int(values[0]), int(values[1]))
你可以用
command, *values = input().split(' ')
值是一个列表。例如,“插入”零件变为:
if command == 'insert':
my_list.insert(int(values[0]), int(values[1]))
在列表之前使用*
将列表转换为函数的参数
如果没有定义默认值,使用函数是一种很好的方法
在列表之前使用*
将列表转换为函数的参数
使用函数是一种很好的方法,可以在未定义默认值的情况下使用默认值。发生此错误是因为您总是希望命令有3个输入:
command, i, e = input().split(' ')
这就是使用“仅打印”时发生的情况:
>>> "print".split(' ')
['print']
因此,input.split()
的输出是一个只有一个元素的列表。然而:
command, i, e = input().split(' ')
需要3个元素:command
、i
和e
其他答案已经向您展示了如何解决修改代码的问题,但随着您添加更多命令,它会变得相当笨拙。您可以使用Python的本机REPL并创建自己的提示符。()
例如:
$ python main.py
Starting prompt...
> insert 2
> insert 3
> insert 4
> print
['2', '3', '4']
>
cmd还允许您记录代码,因为我没有为print
制作docstring,这是我在终端中键入help
后显示的内容:
> help
Documented commands (type help <topic>):
========================================
help insert quit
Undocumented commands:
======================
print
>帮助
文档化命令(键入帮助):
========================================
帮助插入退出
未记录的命令:
======================
打印
我将为您添加其他命令和一个有趣的练习。:) 发生此错误是因为您总是希望命令有3个输入:
command, i, e = input().split(' ')
这就是使用“仅打印”时发生的情况:
>>> "print".split(' ')
['print']
因此,input.split()
的输出是一个只有一个元素的列表。然而:
command, i, e = input().split(' ')
需要3个元素:command
、i
和e
其他答案已经向您展示了如何解决修改代码的问题,但随着您添加更多命令,它会变得相当笨拙。您可以使用Python的本机REPL并创建自己的提示符。()
例如:
$ python main.py
Starting prompt...
> insert 2
> insert 3
> insert 4
> print
['2', '3', '4']
>
cmd还允许您记录代码,因为我没有为print
制作docstring,这是我在终端中键入help
后显示的内容:
> help
Documented commands (type help <topic>):
========================================
help insert quit
Undocumented commands:
======================
print
>帮助
文档化命令(键入帮助):
========================================
帮助插入退出
未记录的命令:
======================
打印
我将为您添加其他命令和一个有趣的练习。:) 看看Python的内置range
函数。根据定义,已知迭代次数的循环更适合for
循环,而不适合while
循环<对于范围内的x(命令数):
将允许您删除x=0
和x=x+1
行。另外,x=x+1
通常写为x+=1
。看看Python的内置range
函数。根据定义,已知迭代次数的循环更适合for
循环,而不适合while
循环<对于范围内的x(命令数):将允许您删除x=0
和x=x+1
行。另外,x=x+1
通常写为x+=1
。如果有人想知道那颗星:如果有人想知道那颗星: