-1
for day_num in range(1,8):
        sales=float(input("Enter the sales for Day {}".format(day_num)))
        bakery_temp_info.append(sales)
    bakery_sale.append(list(bakery_temp_info))
    del bakery_temp_info[:]

这是我的老师给我的示例代码的一部分。任何人都可以解释变量“day_num”在之前没有定义的情况下如何用于这个循环。我尝试在循环中将其打印出来,每次循环运行时它都会增加 1。任何帮助表示赞赏,谢谢。

4

2 回答 2

2
于 2018-09-09T22:44:59.313 回答
0

You are declaring day_num in the loop. Basically the loop is telling the interpreter that you want to iterate over something and want to give each iteration a name, in this case day_num.

The iterable you are providing in this case is a range from 1-8, but you could just as easily pass it a list. In that case you would be calling each element of the list day_num, as an example.

Every time you run through the loop, it updates the value stored in day_num.

于 2018-09-09T22:45:39.607 回答