Range function in python

。_饼干妹妹 提交于 2021-02-08 10:43:12

问题


for n in range(2,5):
    for x in range(2,n):
        print(n,x)

Gives output as:

3 2
4 2
4 3

why the value of n start from 3 not 2?


回答1:


n starts at three because range(2, 2) is empty. Maybe you really want:

for n in range(2, 5):
    for x in range(2, n + 1):
        print(n, x)

Results:

2 2
3 2
3 3
4 2
4 3
4 4


来源:https://stackoverflow.com/questions/50575700/range-function-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!