Is there any way to create multiple variables using a for loop?

0

Issue

I’ve recently taken to using an f-string when I input things into lists using for loops – just for better quality of life:

for i in range(3):
array.append(int(input(f"Please input the value for item {i + 1}: ")))

Or:

array = list(int(input(f"Please enter a value for input {x + 1}: ")) for x in range(3))

This got me wondering whether there was a way to create multiple variables in a similar fashion:

for i in range(3):
input{i} = int(input(f"Please input the value for item {i + 1}: "))

Never seen this done before and I don’t see a way it could be, but hey, would be cool.

Solution

The closest there is to that is using a dictionary:

values = {}

for i in range(3):
    values[i] = int(input(f"Please input the value for item {i + 1}: "))

Answered By – Josh Ackland

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More