-3

Is there a way to make variables with a for statement using the variable from the for statement in order to add a number to the end of the variable name?

Here is an example of what I'm trying to achieve:

for i in range(1,9):
    grade+i = round(float(input(firstprompt)))
    type+i = input(secondprompt)
martineau
  • 119,623
  • 25
  • 170
  • 301
Daniel
  • 63
  • 5
  • 3
    See the link from @VanPeer for two reasons: First, it tells you a much better way to handle arbitrary numbers of values with dynamic names (in Python, use a list or a dictionary, depending on whether an integer index is sufficient or a static name per value is required). Second, it gives good reasons _never_ do what you're asking how to do, in any language, whether it's well-supported or not. – kungphu Nov 01 '18 at 03:19
  • So... what's wrong with a list? – Mateen Ulhaq Nov 01 '18 at 03:22
  • Also see [How can you dynamically create variables via a while loop?](https://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-via-a-while-loop) (even though it's marked as a "dup'). In addition, there's [Python - Dynamic variables](https://stackoverflow.com/questions/11964039/python-dynamic-variables). Get the hint? – martineau Nov 01 '18 at 03:29

2 Answers2

0

I think it is better for you to use list or dict object for that instead

For list

grade = []
type = []
for i in range(1,9):
    grade.append(round(float(input(firstprompt))))
    type.append(input(secondprompt))


For dict

grade = {}
type = {}
for i in range(1,9):
    grade["grade"+str(i)] = round(float(input(firstprompt)))
    type["type"+str(i)] = input(secondprompt)
Andreas
  • 2,455
  • 10
  • 21
  • 24
  • The `grade+i` and `type+i` for a dictionary aren't going to work...you can't add an integer to a dictionary. – martineau Nov 01 '18 at 03:35
  • I edited the code for that – Andreas Nov 01 '18 at 03:45
  • Unfortunately your edit doesn't make it work—you can't add (or concatenate) the integer value of the variable `i` to the strings `"grade"` and `"type"` like that (it raises a `TypeError`). – martineau Nov 01 '18 at 15:39
0

I thought i wouldn't really be possible, but I realized you could use exec in a for loop to achieve that effect:

prompt1 = 'Enter a number:\n'; prompt2 = 'Enter a string:\n' for i in range(1, 9): exec('grade{} = int(input(prompt1))'.format(i)) exec('type{} = input(prompt2)'.format(i))

Although as Andreas suggested, a list would probably be better.

kaboom
  • 50
  • 9
  • 3
    If one were hell-bent on doing this, I would not recommend exec. There's cheaper and safer ways such as modifying the namespace dicts https://stackoverflow.com/a/1373201/365102. – Mateen Ulhaq Nov 01 '18 at 03:23
  • Yeah, I get that exec is rarely used and not that great. The question did ask for this solution, and from the link above it does say that even locals() can't edit variables even if globals could. I also mentioned it wasn't the best solution. – kaboom Nov 01 '18 at 03:46