Python Forum
Create, assign and print variables in loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create, assign and print variables in loop
#1
I want to create and assign a value to a variables in a for loop and name the variable in the loop. This works as intended. Afterwards, I would like to print the variables something like:
a = 3
source=(0.1, 0.2, 0.3)
for n in range (a):
    exec("var"+str(n)+" = source[n]")
    print('var'+str(n))
However, I end up with:
var0
var1
var2
Whereas I want output corresponding to this:
print(var0)
print(var1)
print(var2)
How do I change the code accordingly?
Reply


Messages In This Thread
Create, assign and print variables in loop - by steven_tr - May-28-2020, 11:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 1,117 Nov-14-2023, 08:46 AM
Last Post: perfringo
  How to create a variable only for use inside the scope of a while loop? Radical 10 2,084 Nov-07-2023, 09:49 AM
Last Post: buran
Question How create programmatically variables ? SpongeB0B 6 1,472 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
  How to print variables in function? samuelbachorik 3 980 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,574 Jul-27-2022, 08:50 PM
Last Post: rob101
  Create array of values from 2 variables paulo79 1 1,158 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,576 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  How to create 2 dimensional variables in Python? plumberpy 5 1,967 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  how to use 3 variables python loop evilcode1 2 1,734 Nov-12-2021, 11:43 AM
Last Post: jamesaarr
  How can I assign "multiple variables" to a single "value"? Psycpus 2 1,922 Oct-04-2021, 03:29 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020