0

I would like to add strings to my code in runtime depending on user input.

something like:

import random
set = (1,2,3,4)
x = 1
def newstring():
   etc = random.choice(set)
   (no,x) = etc  
   x = x + 1
   nextstring = input("Next string?")

nextstring = input("Next string?")

while nextstring == "y":
    newstring()
    print(no + x)

I would like to make (n,x) a variable.

So every time the user asked for a new string it would set a new variable like no1 = 4 where 1 is 1 because x is 1 and 4 is 4 because it's a random pick. The new variable then would be no2 with a totally different value

Botond Horváth
  • 55
  • 1
  • 1
  • 6
  • Use a list or a dictionary. – Kevin Apr 03 '15 at 14:10
  • Kevin could you please be just a little more accurate, I'm not quite sure what you mean by that. I mean how do I define a variable with variable in the name, a variable what changes along the code? – Botond Horváth Apr 03 '15 at 14:17
  • The only two ways I know how to do this aside from dictionaries (modifying `locals` and using `exec`) are labeled in big clear letters **NOT RECOMMENDED** – jkd Apr 03 '15 at 14:42

0 Answers0