Python “for i in” + variable

后端 未结 3 1048
一整个雨季
一整个雨季 2021-01-26 23:31

I have the following code:

 #Euler Problem 1

    print \"We are going to solve Project Euler\'s Problem #1\"

    euler_number = input(\'What number do you want         


        
相关标签:
3条回答
  • 2021-01-26 23:55

    In Python, a for loop loops using a series of values. These can be, for example, items in a list; or these can be values that come from an "iterator".

    for i in 3 makes no sense in Python, as 3 is not a series of values.

    To loop over a series of integers, use range() or xrange().

    How does the Python's range function work?

    0 讨论(0)
  • 2021-01-26 23:56

    Here's the documentation for Python's for syntax: http://docs.python.org/2/reference/compound_stmts.html#for

    It loops over any sequence of values, not just a sequence of numbers like in other languages.

    Have fun learning Python, it's a great language. Also, the Project Euler challenges are fun, so stick with them, and don't give up!

    0 讨论(0)
  • 2021-01-27 00:10

    You probably want this:

    for i in range(1, euler_number + 1):
    
    0 讨论(0)
提交回复
热议问题