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.
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
python - how to loop through a list of integers - Stack Overflow
Is there a clean way to treat "i" in a for loop as an integer? (Example code inside)
How to create a for loop using an integer input??
Videos
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")
Try 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.
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 + 2Hello! 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!")
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]
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")