EMic17
EMic17

Reputation: 23

unpacking result of split() into fewer variables than specified

I've been doing a bit of code where I enter some numbers,split them up into separate variables and then orders those numbers, and i can split them up fine, but when I try to split into only 4 (for example) numbers (less than the amount of variables) it returns the error 'not enough variables to unpack'. I want it so I can input the any amount of numbers (up to the max, which in this case is 10) and it will sort the given numbers, print them, but it wont print the other variables.

I've so far done this:

a, b, c, d, e, f, g, h, i, j = input("enter up to 10 digits").split()
nlist=(a, b, c, d, e, f, g, h, i, j)
def bubbleSort():
    for l in range(len(nlist)-1,0,-1):
        for k in range(l):
            if nlist[k]>nlist[k+1]:
                temp = nlist[k]
                nlist[k] = nlist[k+1]
                nlist[k+1] = temp

bubbleSort(nlist)
print(nlist)

I know this is probably has a lot of mistakes, so if anyone could help me find a much more efficient/correct way of doing this, I would greatly appreciate it.

Also, just wondering, how could i do it so once i`ve inputed the numbers i can then find the mean, mode, medium and range of the numbers?

Upvotes: 2

Views: 103

Answers (4)

T.Nel
T.Nel

Reputation: 1588

First you want to get the input as a list

input_list = map(int, input("enter up to 10 numbers").split())

Then you want to sort it:

input_list.sort()

or if you want to keep a version with original order :

sorted_list = sorted(input_list)

Then you can print it:

print(sorted_list)

If you want to limit your user to 10 input, use a loop for the input :

continue_ = True
while continue_:
    input_list = input("enter up too 10 numbers").split()
    continue_ = len(input_list) >= 10

The final code is :

continue_ = True
while continue_:
    input_list = map(int, input("enter up too 10 numbers").split())
    continue_ = len(input_list) >= 10
sorted_list = sorted(input_list)
print(sorted_list)

Upvotes: 0

mshsayem
mshsayem

Reputation: 18018

Other answers have good explanation. I am just showing a way. What are you trying to achieve can be done by this way:

print(sorted(map(int,input().split())))

Short explanation:

  • input takes input string of numbers separated by spaces
  • split() will split those into list of number strings (still str)
  • map(int, ...) will convert those strings to int (now they are numbers)
  • sorted will sort the numbers
  • print will print

And here is a fancy version of the above to restrict input to 10 numbers:

print('Enter 10 numbers. Press Enter after each number:')
print(sorted(int(f()) for f in [input]*10))

Upvotes: 0

Ryan Haining
Ryan Haining

Reputation: 36882

It seems you're confused on what is required of you to use split. On your first two lines you are unpacking a list, then immediately reconstructing the list:

a, b, c, d, e, f, g, h, i, j = input("enter numbers").split()
nlist=(a, b, c, d, e, f, g, h, i, j) # note you actually want [] not ()

If you replace () around your nlist with [] (since you are mutating the list) that would be equivalent to the following for your valid cases

nlist = input("enter numbers").split()

You don't need to unpack at all. Note, however, that nlist will still be a sequence of strs, if you want a list of ints you have a couple of options.

# 1) list comprehension
nlist = [int(i) for i in input("enter numbers").split()]

# 2) map
nlist = map(int, input("enter numbers").split())

Additional issues include:

1) You are defining bubbleSort to take zero arguments, but then calling it with one. nlist is a global which isn't great. You'd be better off dividing your code into a couple of functions

def bubbleSort(nlist):
    ...

def main():
   nlist = # choose from the above options
   bubbleSort(nlist)
   print(nlist)

if __name__ == '__main__': # only run if module is being run directly
    main()

2) You can't assign to elements of a tuple. A tuple is a sequence surrounded by () as you have in your original code for nlist. This won't work

>>> items = (1, 2, 3)
>>> items [0] = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

use a list instead by surrounding the sequence with []

>>> items = [1, 2, 3]
>>> items [0] = 4 # fine

3) Multiple assignments in python mean you don't have to use the old swap idiom. instead of code like

temp = a
a = b
b = temp

You can just write

a, b = b, a

This technique can clean up your code a bit where you are moving elements around the list

Upvotes: 4

kchawla-pi
kchawla-pi

Reputation: 449

If that's your goal and this is not a practice exercise you are doing then do this:

nums = sorted([int(num) for num in input('Enter the numbers here:').split()])

Upvotes: 0

Related Questions