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?
rangefunction. A loop where the number of iterations are know is by definition better fit for aforloop than for awhile.for x in range(number_of_comands):would allow you to remove bothx = 0andx = x + 1lines. Alsox = x + 1is usually written asx += 1.