I am writing the following code in Python to execute a program that 'skips' out numbers in a range of 1-10 which are divisible by 3:
for i in range(10):
while i % 3 == 0 :
i = i + 1
continue
print(i)
BUT,
the output is printing out duplicate values:
1
1
2
4
4
5
7
7
8
10
Can someone please explain the error in the code?
Thanks.
A program that 'skips' out numbers in a range of 1-10 which are divisible by 3:
for i in range(10):
while i % 3 == 0 :
i = i + 1
continue
print(i)
OUTPUT:
0
1
2
4
5
7
8
Hi #Tech_Nut Welcome to Stackoverflow Since your question states numbers from range 1 to 10 then it rules out 0 as a response in answer also the answer by #Piyush is correct but you should look more into loops and check how they work as for and while can be used together but in different scenarios and not like the range of numbers asked by you.
Correct code to do the required operation is mentioned below, please see if this solves your purpose.
for i in range(10):
if i % 3 == 0:
continue
print(I)
Related
I'm attempting to write a program called multChart(x,y) that prints a multiplication table based on two inputs, one specifying the number of rows to print and another specifying the number of columns. So it would look like this:
>>> multChart(4,5):
1: 1 2 3 4 5
2: 2 4 6 8 10
3: 3 6 9 12 15
4: 4 8 12 16 20
Here's what my current code looks like:
def multChart(x,y):
for i in range(1,x+1):
print(i,':',i*1,i*2,i*3,i*4,i*5)
I'm totally stuck on how to implement the y value. I also know there should be a better way of printing the multiplication instead of i * multiples of five, but I'm not sure what loop to use. Any help would be greatly appreciated.
You need another loop inside your print for looping over the y range:
def multChart(x, y):
for i in range(1, x+1):
print(i, ':', *[i * z for z in range(1, y+1)])
def multChart(x,y):
for i in range(1,x+1):
print(i, ':', end=" ")
for j in range(1,y+1):
print(i*j, end =" ")
print()
multChart(4,5)
produces
1 : 1 2 3 4 5
2 : 2 4 6 8 10
3 : 3 6 9 12 15
4 : 4 8 12 16 20
You can use a second for loop for the second index. Also, note that you can use end in the print statement.
def multChart(x,y):
for i in range(1,x+1):
print(i,':',*list(map(lambda y: i*y,list(range(1,y+1 ) ) ) ) )
multChart(4,5)
This question already has answers here:
Skip multiple iterations in loop
(7 answers)
Closed 3 years ago.
I have a for loop in range(100).
Whenever I find a number that does satisfy a condition, it should skip a loop certain number of times
for i in range(2,100):
if i %2 != 0:
i = i+3
Expected results should be:
2
3
6
7
10
11
.
.
.
.
.
A for-loop does not allow you to skip multiple cycles. You can only use
continue to skip the rest of the current cycle
break to skip all remaining cycles and exit the loop
What you need is a while-loop. Where a for-loop defines a number that is incremented from a starting value up to a limit, a while-loop only needs an exit-condition.
Try this:
i = 2
while i < 100:
if i % 2 == 0:
i += 4
else:
i += 1
In my understanding this is what you want to do. But its not the output you desire. But I guess your expected output is not the output of your logic.
i = 2
while i<100:
print(i)
if i % 2 != 0:
i += 3
else:
i += 1
Outputs:
2
3
6
7
10
11
...
j = 0
for i in range(j,100):
if i%2 !=0:
j = i+3
continue
print(j)
output:
0
4
6
8
10
12
14
16
18
20
.
.
.
.
Actually i = i+3 has no effect in a for loop. you may think that i becomes 2+3=5 but in the next iteration i will be 3. You need to use a hile loop for this. This will do the trick for you:
i = 2
while i <= 100:
print(i)
i = (i + 3) if i % 2 != 0 else (i + 1)
Solution with a for loop (although I think a while loop is better for this):
skipping = 0
for i in range(2, 100):
if skipping:
skipping -= 1
continue
if i % 2 != 0:
print(i)
skipping = 2
else:
print(i)
Output:
2
3
6
7
10
11
...
I am stuck in a program where I have to print "Block" before every 5 numbers without using an additional variable.
This is the code:
for index,i in enumerate(range(1,11)):
print(i)
expected output:
Block
1
2
3
4
5
Block
6
7
8
9
10
Please help new to python.
The expression x % 5 will give you zero if and only if x is a multiple of five.
So, if you wanted to output "Block" before 1, 6, 11, ..., you could use:
if (i - 1) % 5 == 0: print('Block')
before printing the number.
In other words, it's as simple as:
for i in range(1, 11):
if (i - 1) % 5 == 0: print('Block')
print(i)
Running that program gives your expected output of:
Block
1
2
3
4
5
Block
6
7
8
9
10
Note that this will only work if you start at one (as it appears you do). Any other start point will require a slightly modified solution.
In python this can be done by
For index,I in enumerate (range(1,21)):
Print(i)
if(i%5==0):
Print("block")
I want to make a program that counts down from 10 by using the operator while.
This is what I have so far but it would not do what I want.
i = 10
while i < 10:
print(i)
i = i-1
This doesn't work and I'd like to know why!
You exit the loop as soon as i is less than 10. 9 is less than 10 so you leave the loop in the first run. What you need is:
i = 10
while i > 0:
print(i)
i = i-1
In this case you stay in the loop as long as i is greater than zero.
i < 10 means "i is less than 10"
i > 10 means "i is greater than 10"
The symbol is a bit like an arrow. It always points towards the smaller item. This is slightly different if you are checking for a condition.
if i < 10 is saying "if i is less than 10"
while i > 10 is saying, "while i is greater than 10"
So this is what you need:
i = 10
while i > 0: # while i is greater than zero
print(i)
i = i - 1 # i will eventually be less than zero, meaning the
# condition for the loop will return false and stop the loop
This will print i from 10 to 0
Others have shown you the way how to do it with while. My question is why do you want to do it by using while. If there are no constrains, I would suggest you to look for range:
https://docs.python.org/3.5/library/functions.html#func-range
You could implement your counting like this:
for i in range(11):
print(i)
#output
0
1
2
3
4
5
6
7
8
9
10
To count down just reverse the range function:
for i in reversed(range(11)):
print(i)
#output
10
9
8
7
6
5
4
3
2
1
0
I hope this alternative can help you.
I'm supposed to align some numbers in a table while using a loop inside a loop. It should look like this when it's done:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
What I've stared with is:
for i in range(1,10,1):
for c in range(1,6,1):
print(i, end='\n')
Though after running this it just prints them bellow each other. And replacing "\n" with "\t" doesn't help either. I've tried .format but with no success. Sorry if this seems very simple and that it should be stated somewhere but I can't find someone with the same problem or some chapter referring to this specific problem.
I did manage to do this table by using while like this:
i = 1
while i < 6:
print("1 2 3 4 5 6 7 8 9\n" , end='' '')
i += 1
This is of course not the best way to do it, I just don't have the knowledge to do it in a smarter way.
First, you have inverted loop ranges it should be:
for i in range(1, 6, 1):
for c in range(1, 10, 1):
Second, in the inner loop you have to print c, not i.
The full code would be like this:
for i in range(1, 6, 1):
for c in range(1, 10, 1):
print(c, end=" ")
print('')
If you want to code only one for loop you can:
for i in range(1, 6, 1):
print( " ".join(map(str, range(1, 10, 1))) )
Firstly, your loops are in the wrong order. As you write your code, you will get 10 rows with 6 numbers.
Next, you can't print in the inner loop, because you will get just 60 rows. So you can store rows in some temp value inside first loop, and on each cycle of outer loop print it. Also, for more pretty code, you can use list comprehensions.
for i in range(1, 10):
print ' '.join([str(number) for number in range(1, 6)])
Also, to perfect this, you can write it in 1 string of code, but this is more understandable.