英文:
Save Loop result into a variable?
问题
我有一个带有循环的代码:
while j < 25:
print('#', j, ":", head[head.index(minHead) - j])
j = j + 1
这将打印出以下结果:
# 0 : 12.148057
# 1 : 15.776696999999999
# 2 : 28.555822
# 3 : 28.89765
# 4 : 28.923944999999996
# 5 : 28.950239
# 6 : 28.950239
# 7 : 28.950239
# 8 : 28.950239
# 9 : 28.923944999999996
# 10 : 28.923944999999996
# 11 : 28.923944999999996
# 12 : 28.923944999999996
# 13 : 28.923944999999996
# 14 : 28.923944999999996
# 15 : 28.923944999999996
# 16 : 28.923944999999996
# 17 : 28.923944999999996
# 18 : 28.923944999999996
# 19 : 28.923944999999996
# 20 : 28.923944999999996
# 21 : 28.950239
# 22 : 28.950239
# 23 : 28.950239
# 24 : 28.950239
我可以保存所有这些 #24 索引值,并为它们命名,以便在 tkinter 小部件中显示它们作为信息文本吗?
英文:
I have a code with a loop:
while j < 25:
print ('#',j, ":", head[head.index(minHead)-j])
j = j + 1
That gives as result printed:
# 0 : 12.148057
# 1 : 15.776696999999999
# 2 : 28.555822
# 3 : 28.89765
# 4 : 28.923944999999996
# 5 : 28.950239
# 6 : 28.950239
# 7 : 28.950239
# 8 : 28.950239
# 9 : 28.923944999999996
# 10 : 28.923944999999996
# 11 : 28.923944999999996
# 12 : 28.923944999999996
# 13 : 28.923944999999996
# 14 : 28.923944999999996
# 15 : 28.923944999999996
# 16 : 28.923944999999996
# 17 : 28.923944999999996
# 18 : 28.923944999999996
# 19 : 28.923944999999996
# 20 : 28.923944999999996
# 21 : 28.950239
# 22 : 28.950239
# 23 : 28.950239
# 24 : 28.950239
Can I save all this #24 index values with a name in order to show all of them as info text inside a tkinter widget?
答案1
得分: 1
你需要创建一个变量来保存来自while循环的值,例如:
list_of_values = []
j = 0
while j < 25:
string_to_save = '#', j, ':', j
print(string_to_save)
j = j + 1 # 这可以写成 j += 1
list_of_values.append(string_to_save)
我移除了变量head、minHead等,因为您的代码没有定义这些对象。但上面的代码片段应该足够让您走上正确的道路。
如果您对tkinter有单独的问题,请随时提出单独的问题。
英文:
You'll need to create a variable to save the values from your while loop e.g.:
list_of_values = []
j = 0
while j < 25:
string_to_save = '#',j, ":", j
print (string_to_save)
j = j + 1 # this can be written as j += 1
list_of_values.append(string_to_save)
I removed the variables head, minHead etc. as your code doesn't define what these objects are. But the snippet above should be enough to get your on the right track.
If you have a separate problem with tkinter feel free to ask a separate question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论