Try using this:

for k in range(1,c+1,2):
Answer from carlosdc on Stack Overflow
🌐
Python
peps.python.org › pep-0284
PEP 284 – Integer for-loops | peps.python.org
March 1, 2002 - One of the most common uses of for-loops in Python is to iterate over an interval of integers. Python provides functions range() and xrange() to generate lists and iterators for such intervals, which work best for the most frequent case: half-open ...
Discussions

python - I want to get an integer input from a user and then make the for loop iterate through that number, and then call a function that many times - Stack Overflow
First, input returns a string, whereas you want an int. You'll need to do a conversion. Second, for loops only take iterables, not integers, so you want to use range to get the values to iterate over. More on stackoverflow.com
🌐 stackoverflow.com
python - how to loop through a list of integers - Stack Overflow
I'm trying to loop through a list of integers by using a for loop. However, we know that in python, int is not iterable. So I'm wondering if there's any ways I can store integers in another way so ... More on stackoverflow.com
🌐 stackoverflow.com
Is there a clean way to treat "i" in a for loop as an integer? (Example code inside)
Rather than have a counting variable, you could enumerate: for index, value in enumerate(fighters): print(index, value) 0 ryu 1 ken 2 zangief 3 balrog 4 chun-li 5 blanka 6 sagat 7 ehonda For your given example, though, you could zip: for first, second in zip(fighters, fighters[2:]): print(first, second) ryu zangief ken balrog zangief chun-li balrog blanka chun-li sagat blanka ehonda You could also check out combinations , although it'll produce more pairings than your current version, it's a bit easier to use. Edit: Editor mangling. Edit: u/CylonSaydrah has suggested the following improvement to the zip version, which uses a step of 2 to skip elements in each list: for first, second in zip(fighters[::2],fighters[1::2]): More on reddit.com
🌐 r/learnpython
53
116
February 26, 2019
How to create a for loop using an integer input??
Replace the 0 in count = 0 with your integer. count = 0 for i in range(0,count): print("Python") More on reddit.com
🌐 r/learnprogramming
23
12
February 4, 2025
🌐
Snakify
snakify.org › for loop with range
For loop with range - Learn Python 3 - Snakify
for loop iterates over any sequence. For instance, any string in Python is a sequence of its characters, so we can iterate over them using for: ... Another use case for a for-loop is to iterate some integer variable in increasing or decreasing order.
🌐
Reddit
reddit.com › r/learnpython › is there a clean way to treat "i" in a for loop as an integer? (example code inside)
r/learnpython on Reddit: Is there a clean way to treat "i" in a for loop as an integer? (Example code inside)
February 26, 2019 -

I wrote an example code. I want to treat "i" in the for loop as an integer. In this case I needed it to index though a list. I find myself writing a "counting variable" really often, and I was wondering if there was a better way to write it.

#this program will form pairs of fighters from the fighters list 

fighters = ['ryu', 'ken', 'zangief', 'balrog', 'chun-li', 'blanka', 'sagat', 'ehonda']

count = 0

for i in range(int(len(fighters)/2)):
     print(fighters[count] + " will fight " + fighters[count + 1])
     count = count + 2
Top answer
1 of 5
106
Rather than have a counting variable, you could enumerate: for index, value in enumerate(fighters): print(index, value) 0 ryu 1 ken 2 zangief 3 balrog 4 chun-li 5 blanka 6 sagat 7 ehonda For your given example, though, you could zip: for first, second in zip(fighters, fighters[2:]): print(first, second) ryu zangief ken balrog zangief chun-li balrog blanka chun-li sagat blanka ehonda You could also check out combinations , although it'll produce more pairings than your current version, it's a bit easier to use. Edit: Editor mangling. Edit: u/CylonSaydrah has suggested the following improvement to the zip version, which uses a step of 2 to skip elements in each list: for first, second in zip(fighters[::2],fighters[1::2]):
2 of 5
11
Short answer: i is already an integer. Long Answer The range() built-in allows you iterate over a range of numbers, so in the sequence for i in range(10), i starts at 0 and counts up to 9 for the duration of the loop. >>> for i in range(10) ... print(f"{type(i)} {i}") ... 0 1 2 # -- 8< SNIP --- Another note is that it's not necessary to call int() on len(fighters) / 2 if you want to truncate the result; instead, use the floor division operator // (IIRC it always rounds down). Anyway, for the code snippet you provided, you don't need to use // anyway as you can just leverage range()'s step parameter. range() doesn't take keyword arguments, so you have to specify the start index (0) as well: for i in range(0, len(fighters), 2): print(fighters[i] + " will fight " + fighters[i+1]) However, if the roster is always going to be ordered the same, you can also couple the fighters in tuples: fighters = [("ruy", "ken"), ("zangief", "balrog"), ("chun-li", "blanka"), ("sagat", "ehonda")]. You may also want to take a look at zip() which does the above: >>> player1 = ["ruy", "zangief", "chun-li", "sagat"] >>> player2 = ["ken", "balrog", "blanka", "ehonda"] >>> zipped = zip(player1, player2) >>> list(zipped) [('ruy', 'ken'), ('zangief', 'balrog'), ('chun-li', 'blanka'), ('sagat', 'ehonda')] Then you just iterate over zipped: >>> for pair in zipped: ... print(pair[0] + " will fight " + pair[1]) EDIT: Thanks to u/warbird2k for pointing out that you can iterate over zipped immediately without wrapping it in a list.
🌐
w3resource
w3resource.com › python › python-for-loop.php
Python for loop
September 20, 2024 - ... In this example, we count the ... = 0 for x in numbers: if x % 2: count_odd+=1 else: count_even+=1 print("Number of even numbers :",count_even) print("Number of odd numbers :",count_odd) ......
Find elsewhere
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-2-for-loop
5.2 For loop - Introduction to Python Programming | OpenStax
March 13, 2024 - The range() function is a common approach for implementing counting in a for loop. A range() function generates a sequence of integers between the two numbers given a step size. This integer sequence is inclusive of the start and exclusive of ...
🌐
Reddit
reddit.com › r/learnprogramming › how to create a for loop using an integer input??
r/learnprogramming on Reddit: How to create a for loop using an integer input??
February 4, 2025 -

Hello! I'm new to coding and I'm learning for loops, and decided to start playing around with them, because why not? I for the LIFE of me can't figure out how to get a for loop to print the word "Python" for the amount of the number I input lol. What I'm essentially trying to do is have an input ask me "How many times can you say python", and whatever my integer input is, iterate "Python" that number of times. This is what I IMAGINE it would look like, but to no avail. (Sorry if this code looks silly, it doesn't even work but I'm trying LOL!)

python_count = int(input("How many times can you say python?"))                   say_python = python_count                                                               for x in say_python:                                                       print("Python!")
🌐
Codementor
codementor.io › community › making python integers iterable
Making Python Integers Iterable | Codementor
December 10, 2020 - In the example below, we see how a list a is iterated through using a for ... in loop and each element can be accessed via variable x. >>> a = [2, 3, 5, 7, 11, 13] >>> for x in a: print(x, end=" ") 2 3 5 7 11 13 · Similar to list, range is a python type that allows us to iterate on integer values starting with the value start and going till end while stepping over step values at each time.
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-for-loop-in-python
How to use for loop in Python?
March 10, 2021 - Python's list object is also an indexed sequence, and hence you can iterate over its items using a for loop. In the following example, the for loop traverses a list containing integers and prints only those which are divisible by 2.
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
🌐
Reddit
reddit.com › r/learnpython › why is my for loop not an integer?
r/learnpython on Reddit: Why is my for loop not an integer?
June 24, 2022 -

Why does it have to be a range of the length of the word in this code in the line of code highlighted below? I get a TypeError: string indices must be integers when I take out the range(len portion of this code for this line of code. It seems that when the program is looping through each index, is it not an integer of that index? Why do I then need the range function. I'm on the 100 days of code course and even though this isn't really touched on in this lession, I'm trying to figure out why I have to use the range and len function. Thanks.

for i in range(len(random_computer_word)):




import random

word_list = ["Aardvark", "Baboon", "Camel"]

random_computer_word = random.choice(word_list).lower()

display = []

end_of_game = False

for x in random_computer_word:
    display += "_"

while end_of_game == False:
    user = input("Please guess a letter").lower()
    for i in range(len(random_computer_word)):
        letter = random_computer_word[i]
        if letter == user:
            display[i] = letter
    print(display)
    if "_" not in display:
        end_of_game = True
    print("This is the end of the game")
🌐
Tutorialspoint
tutorialspoint.com › python › python_for_loops.htm
Python - For Loops
Python's built-in range() function returns an iterator object that streams a sequence of numbers. This object contains integers from start to stop, separated by step parameter. You can run a for loop with range as well.
🌐
W3Schools
w3schools.com › python › gloss_python_for_range.asp
Python Looping Through a Range
Python For Loops Tutorial For Loop Through a String For Break For Continue For Else Nested Loops For pass
🌐
Python.org
discuss.python.org › python help
For Loop in Python - Python Help - Discussions on Python.org
September 7, 2023 - Pardon if I have been posting series of questions. Newbie Python learner here, and am going through PCEP materials, and having some questions along the way… Thank you in advance for the help. In these two codes, I confused why the answer is such: Code 1: my_list = [0,1,2,3] x = 1 for elem in my_list: x *= elem print (x) Answer: [0] Code 2: my_list = [0,1,2,3] x = 1 for elem in my_list: new_list = x * elem print (new_list) Answer: [3] Question 3: I want to create a code that multi...
🌐
Python Course
python-course.eu › for_loop.php
20. For Loops | Python Tutorial | python-course.eu
So the definition is very simple: Three integers satisfying a2+b2=c2 are called Pythagorean numbers. The following program calculates all pythagorean numbers less than a maximal number. Remark: We have to import the math module to be able to calculate the square root of a number. from math ...
🌐
PYnative
pynative.com › home › python › python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - Python range() function generates the immutable sequence of numbers starting from the given start integer to the stop integer. The range() is a built-in function that returns a range object that consists series of integer numbers, which we can ...