Try using this:
for k in range(1,c+1,2):
Answer from carlosdc on Stack OverflowTry using this:
for k in range(1,c+1,2):
You should also know that in Python, iterating over integer indices is bad style, and also slower than the alternative. If you just want to look at each of the items in a list or dict, loop directly through the list or dict.
mylist = [1,2,3]
for item in mylist:
print item
mydict = {1:'one', 2:'two', 3:'three'}
for key in mydict:
print key, mydict[key]
This is actually faster than using the above code with range(), and removes the extraneous i variable.
If you need to edit items of a list in-place, then you do need the index, but there's still a better way:
for i, item in enumerate(mylist):
mylist[i] = item**2
Again, this is both faster and considered more readable. This one of the main shifts in thinking you need to make when coming from C++ to Python.
How to create a for loop using an integer input??
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
Is there a clean way to treat "i" in a for loop as an integer? (Example code inside)
For Loop in Python
Videos
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!")
First of all you need to convert the input to integer:
timestoprint = int(input("How many times to print hello?"))
Then you have to use range builting generator and use it
for x in range(timestoprint):
printHello()
How does range work?
You have the option to provide arguments to range in any of the following configurations:
range(number) which generates counting from 0 to number-1 at increment of 1
range(start, stop) which generates counting from start to stop-1 at increment of 1
range(start, stop, step) which generates counting from start to stop-1 at increment of step
You might also want to validate the user input which can be done by replacing the input statement with a forever while loop which breaks once the user provides valid input
while True:
timestoprint = input("How many times to print hello?")
if timestoprint.isnumeric(): # Check if input is a number
timestoprint = int(timestoprint) # Convert it to number
break # Break the while loop
else: # if it is not a number
print("The input is not a number.")
You wrote
for i in timestoprint:
printHello()
instead of
for i in range(timestoprint):
printHello()
You also forgot to convert timestopprint to an int
This works for me:
timestoprint = int(input("How many times to print hello?"))
for i in range(timestoprint):
print("hello")
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 + 2Try something like this:
user_input = input("Please enter a bunch of integers: ")
number_list = user_input.split(" ") # or a different delimeter
integers = [int(entry) for entry in number_list]
get_index_of_smallest(integers)
There seems to be a few things you may want to fix in your code:
- Do not use
intas variable name. It has a special meaning in Python and this name should never be used in other ways. - If you expect user to input a bunch of numbers you cannot directly cast input to int. It depends on how the numbers will be inputed but you first need to separate them from input string (initially
inputreturns strings). That's what's causing your error. Look at the previous answers for examples. - The logic of your function could use some improvement. If you want to return smallest, non-negative element from your list there is no need to first filter them to separate list.
for x in range(postsint): # start at beginning as mgilson suggested
should work fine. You already casted it an integer, You shouldn't be creating a string from it
it's because "%d" is used for string formatting and it returns string only:
In [18]: type( "%d" % 3)
Out[18]: <type 'str'>
use just ,range(0,4) or xrange(0,4) if you're on python 2.x:
In [19]: range(0,4) #0 is the default value of the first argument, so range(0,4)==range(4)
Out[19]: [0, 1, 2, 3]