接下来,假设输出格式。在我看来,这种假设有两个主要问题:
1)调用while循环中的函数(例如print())在调用它们时,可以毫不延迟地查看未来的传递循环是否会为函数提供额外的数据。在
2)假设Python解释器会猜测打印的数字(在while循环中)希望以逗号分隔的列表形式返回。计算机是按照你的指令去做的机器如果你不写逻辑来解释你需要什么,机器就不能给你这个。在
您可以通过以下方式表达您想要的输出:
1)收集列表中的数字(作为字符串),然后在循环完成后输出:square = 0
number = 1
# think of this as a result container
number_result_list = []
while number < 10:
square = number ** 2
# place that number at the end of your container:
number_result_list.append(str(square))
number += 1
# join all your number strings together, using a comma
print(",".join(number_result_list))
# prints 1,4,9,16,25,36,49,64,81
2)指定要在打印调用中使用逗号。特别注意后面的逗号,你现在知道为什么会这样了:
^{pr2}$