Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}: "))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading