1

I need to change the text in the variable after one round of the cycle. Like first round of loop a="A", second round a="B".

a = ("A")
a1 = ("B")
a2 = ("C")
a3 = ("D")
a4 = ("F")

i = 0
while i < 5:
    print(a)
    a += 1
    i += 1
1
  • Why don't use cycle from itertools module? Like cycle('ABCDEF) is the best use case. Commented Jul 7, 2022 at 11:28

3 Answers 3

3

Put the values in an array and access the values:

a = ['A', 'B', 'C', 'D', 'E']

i = 0
while i<5:
    print(a[i])
    i += 1
Sign up to request clarification or add additional context in comments.

Comments

0

Increasing a by one (in the line a += 1) will increase the contents of the variable, not change the name. You should instead use an array like

a = ['A', 'B', 'C', 'D', 'E']

and then iterate such as through

ans = a[i]
print(ans)

Comments

0

You could use lists in such kind of tasks:

a = "A"
a1 = "B"
a2 = "C"
a3 = "D"
a4 = "F"

values = [a, a1, a2, a3, a4] # This is the list of values that you could address by their index

i = 0
while i < len(values): # iterate while i is less than the amount of values
    print(values[i])
    i += 1

Read more about lists here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.