[英]While loop change variables
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",第二轮循环 a="B"。
a = ("A")
a1 = ("B")
a2 = ("C")
a3 = ("D")
a4 = ("F")
i = 0
while i < 5:
print(a)
a += 1
i += 1
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
a = 'A'
for i in range(5):
print(a)
a = chr(ord(a) + 1)
Increasing a by one (in the line a += 1) will increase the contents of the variable, not change the name.将 a 加一(在 a += 1 行)将增加变量的内容,而不是更改名称。 You should instead use an array like您应该改为使用类似的数组
a = ['A', 'B', 'C', 'D', 'E']
and then iterate such as through然后迭代例如通过
ans = a[i]
print(ans)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.