So i started learning python and thought for an exercise I would try to write up a little script just to see if I could. Turns out I couldn't get it just right and would have left it, but got a little determined and now have a vendetta against this particular function.
I'm wanting to get the code to take a raw input of a given number and from that generate all possible permutations of a list of the numbers up to it. eg. if the input was "5" then it would generate all permutations of length 5 for [1, 2, 3, 4, 5].
What I tried was as follows:
from itertools import permutations
from math import factorial
n = raw_input("Input number to generate permutation list")
factorial_func = factorial(n)
print "there are %s permutations as follows:" %(factorial_func)
print list(permutations([1:n], n))
I know the faulty line is line 10 because of the [1:n] part and i don't know how to get it to make a list from 1 to n and put that into the permutation function. (I was hoping by going [1:n] it would generate a list from 1 to n in the same way that you can use it to access parts of a list from a to b with list_name[a:b] but it seems that isn't the case)
Sorry if this seems really trivial or is an obvious mistake, I only just started trying to learn python a few days ago.
Yeah that is the bad line. What you are doing with the
[1:n]
is called slicing and is completely unrelated to coming up with ranges. Use therange
function instead:(Note the
n+1
. Range is not inclusive of the end number).You also aren't taking input properly.
raw_input
give a string, and you want an int so do: