How can I generate a list of consecutive numbers?

前端 未结 7 764
南旧
南旧 2020-11-27 04:38

Say if you had a number input 8 in python and you wanted to generate a list of consecutive numbers up to 8 like

[0, 1, 2, 3, 4, 5,          


        
7条回答
  •  情话喂你
    2020-11-27 05:13

    Depending on how you want the result, you can also print each number in a for loop:

    def numbers():
        for i in range(int(input('How far do you wanna go? '))+1):
            print(i)
    

    So if the user input was 7 for example:

    How far do you wanna go? 7
    0
    1
    2
    3
    4
    5
    6
    7
    

    You can also delete the '+1' in the for loop and place it on the print statement, which will change it to starting at 1 instead of 0.

提交回复
热议问题