-2

I use this code in python37 and it generates numbers in the list, but I would also like it to create this name along with it for the list name.

The code just does this:

[1,2,3,4]

This is the name I hoping for to add it to it on the fly:

lst1z

Here is the result for what I hope someone can edit the code to make it do this:

lst1z = [1,2,3,4]

So here is my code Thanks:

     composites=[]
     n = int(input("Enter any number : "))
     positive_n = abs(n)
     for num in range(positive_n+1):
         if num > 1:
            for i in range(1, num,1):
                 if (num % i) == 0:
                     if n > 0:
                composites.append(num)
            else:
                composites.append(-num)
            break
      print ("\ncomposites from", int(positive_n / n), " to ", n, "\n", composites)
Edward
  • 21
  • 6
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Thierry Lathuille Dec 03 '19 at 11:23
  • *Why* do you want to do that? what problem are you trying to solve? – Guy Dec 03 '19 at 11:24
  • I'm going to use the list to search for non-Mersenne Prime factors from prime numbers which don't create a Mersenne Prime Number. – Edward Dec 03 '19 at 11:27
  • @Edward My question was why do you want to give the list a dynamic name? – Guy Dec 03 '19 at 11:31
  • Thierry Lathuille; I could not find an answer Thierry. – Edward Dec 03 '19 at 11:32
  • Hi Guy, so it flows with the code I'm creating. – Edward Dec 03 '19 at 12:00
  • @Edward sorry, but I have no idea what you mean. And what do you mean by `The code just does this: [1,2,3,4]`? you should also indent your code properly, currently this program doesn't run. – Guy Dec 03 '19 at 12:13

1 Answers1

0
composites=[]
     n = int(input("Enter any number : "))
     positive_n = abs(n)
     for i,num in enumerate(range(positive_n+1)):
         if num > 1:
             for i in range(1, num,1):
                 if (num % i) == 0:
                     if n > 0:
                composites.append(num)
            else:
                composites.append(-num)
            break
      #If all you need is to create vairalble and print, you can simply do it as follows:
      list1z = composites
      print("\ncomposites from {} to {} \n list1z = {}".format(int(positive_n/n), n, list1z))
      print ("\ncomposites from", int(positive_n / n), " to ", n, "\n", composites)
Faizan Naseer
  • 589
  • 3
  • 12
  • Hi Faizan, Thanks for a quick response, it must be me but the procedure is not naming it at all? – Edward Dec 03 '19 at 11:49
  • Please do not copy/pase the code, just follow the syntax of `exec()` and modify/restructure the code where ever it fits. – Faizan Naseer Dec 03 '19 at 11:55
  • Hi Faizan, I typed it in nothing no result can you show an edit that prints the list of numbers with this name lst1z = [1,2,3] thanks – Edward Dec 03 '19 at 11:59
  • Do you just want to print like that or you want to create a variable with this name and print it along with its value ?? – Faizan Naseer Dec 03 '19 at 12:07
  • Yes to create a variable with this name and print it along with its value yes please – Edward Dec 03 '19 at 12:09
  • I have edited the code. Hope that is what you required. A Suggestion : Please define your question with appropriate input and output expectations. – Faizan Naseer Dec 03 '19 at 12:29