0

I'm trying to allow users to manipulate a list in 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':
        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")

When users enter a command which requires two integers (such as insert), the program works, but when users enter something like print I get the error "not enough values to unpack". It only works if you input it as print 0 0. How could I allow users to enter commands with integers and without integers?

1
  • Take a look at Python's builtin range function. A loop where the number of iterations are know is by definition better fit for a for loop than for a while. for x in range(number_of_comands): would allow you to remove both x = 0 and x = x + 1 lines. Also x = x + 1 is usually written as x += 1. Commented Mar 16, 2018 at 12:26

4 Answers 4

4

You can use

command, *values = input().split(' ')

values is a list. For example the 'insert' part becomes:

if command == 'insert':
    my_list.insert(int(values[0]), int(values[1]))
Sign up to request clarification or add additional context in comments.

1 Comment

If anyone's wondering about that star: stackoverflow.com/questions/400739/…
1
def my_function(command='print', i=0, e=0):
    # your function code here

user_input = input().split(' ')
if len(user_input) > 3:
    print('usage : function i e')
else:
    my_function(*user_input)

Using * before the list converts the list as arguments for your function.

Using a function is a nice way to have default values in case they aren't defined.

Comments

0

Here is where the unpacking is happening:

command, i, e = input().split(' ')

entering "print" only won't allow this line to execute properly, as no values for i and e were provided.

So just read the input, then split it and check how many arguments did the user supply:

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

Comments

0

This error occurs since you're always expecting a command to have 3 inputs:

command, i, e = input().split(' ')

This is what happens when you use just print:

>>> "print".split(' ')
['print']

So the output of input.split() is a list with only one element. However:

command, i, e = input().split(' ')

is expecting 3 elements: command, i and e.

Other answers already showed you how to solve modifying your code, but it can get pretty clunky as you add more commands. You can use Python's native REPL and create your own prompt. (Original post where I read about the cmd module)

from cmd import Cmd

class MyPrompt(Cmd):
    my_list = []

    def do_insert(self, *args):
        """Inserts element in List"""
        self.my_list.append(*args)

    def do_print(self, *args):
        print(self.my_list)

    def do_quit(self, args):
        """Quits the program."""
        raise SystemExit


if __name__ == '__main__':
    prompt = MyPrompt()
    prompt.prompt = '> '
    prompt.cmdloop('Starting prompt...')

Example:

$ python main.py
Starting prompt...
> insert 2
> insert 3
> insert 4
> print
['2', '3', '4']
> 

cmd also lets you document the code, since I didn't make a docstring for print this is what gets shown once I type help in the terminal:

> help

Documented commands (type help <topic>):
========================================
help  insert  quit

Undocumented commands:
======================
print

I leave adding the other commands and an fun exercise to you. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.