How to create python program, multiple of 3 with while loops - python

ok first sorry if my english is bad
i want to ask how to create python programs multiple of 3 with while loop, like this:
i=0
while i < 10:
i += 1
if i == 3:
continue
print(i)
output:
1
2
4
5
6
7
8
9
10
so i want to eliminate the number 3 6 9, can anyone help me ? i newbie :'v thanks.

It seems as if you want to print the numbers in reverse order (backwards/upside-down), so here is the snippet of code to achieve that task:
i=10
while i > 0:
if i%3 != 0:
print(i)
i-=1
The value of 'i' is the number which it will start counting down from.

i=0
while i < 10:
i += 1
if i % 3 == 0:
continue
print(i)

Related

Finding the 1000th prime number (python) debug

I am attempting to write a code to calculate the 1000th prime number and I'm running into an issue with my loop counter that i don't understand.
prime_test = 1
count=0
for count in range(0,1001):
for divisor in range(2,prime_test):
if (prime_test % divisor) == 0:
break
else:
print(prime_test)
count += 1
prime_test+=1
Could someone please explain why the above code is dysfunctional? The problem is that the count variable iterates at the same rate as the prime_test variable. How do I separate the two such that count only increases when a new prime is found and not when the loop is engaged?
Don't use for count in range(0, 1001):. That just increments count sequentially, not when it finds a prime. Use a while loop.
prime_test = 2
count = 0
while count < 1000:
for divisor in range(2,prime_test):
if (prime_test % divisor) == 0:
break
else:
print(prime_test)
count += 1
prime_test += 1
You also should start prime_test at 2, not 1, since 1 isn't a prime number, but your algorithm will say it is.
One more answer as the same thing might need to be repeated thousand times before it could be understood.
Setting the value of c before the loop has no effect at all and the new to c within the loop assigned value will be overwritten by next loop loop as c will be set to the next value provided by range(). Python for c in range() loops are not like loops in some other programming languages. So every newbie has to go through this ... wondering how it comes.
Following code demonstrates this:
c = 100
for c in range(5):
print(c, end=' -> ')
c = c + 5
print(c)
printing
0 -> 5
1 -> 6
2 -> 7
3 -> 8
4 -> 9
If you change your code to:
prime_test = 2
counter=0
for count in range(0,11):
for divisor in range(2, prime_test):
if (prime_test % divisor) == 0:
break
else:
print(prime_test)
counter += 1
prime_test+=1
print(f'{counter=} , {prime_test=} ')
you will get as output:
2
3
5
7
11
counter=5 , prime_test=13

Print list in terminal on multiple lines [duplicate]

This question already has answers here:
How to print X items in a list per line
(3 answers)
Closed 2 years ago.
i am really beginner and just starting programming with python since 2 weeks.
here is my question i cant find anywhere or i cant figure out the solution.
i want to see my result in terminal with multiple line , for example i want to calculate long for loop and the result show up this loop on terminal with very long a single line.
is there any solution for this?
and sorry for my bad english isnt my native language.
Code:
list = list(range(1,100))
for x in list:
if x % 3 == 0 or x % 5 == 0:
print(x, end=' ')
Output:
3 5 6 9 10 12 15 18 20 21 24 25 etc...
But i want this one:
3 5 6 9 10
12 15 18 20 21
You could add an if statement before you print, to determine whether you want to have end be a blank or a newline character.
x_list = range(1,100)
n = 0
for x in x_list:
if x % 3 == 0 or x % 5 == 0:
if n == 4:
print(x, end='\n')
n = 0
else:
print(x, end=' ')
n = n + 1
You can change your print statement to:
print(x)
If you would print some results in the same line, you can do a logic to print some characters inline and after some iteration print in next line.
list1 = list(range(1,100))
for num, x in enumerate(list1):
if x % 3 == 0 or x % 5 == 0:
print(x, end=' ')
if num % 10 == 0:
print()

Using conditional statements in while loop

I was under the impression that adding the following if statement in my while loop makes python pick only the odd values of i that are smaller than 7 and sums them. However, this is not the case.
Here's my code:
i = 0
sum = 0
while i < 7:
if (i % 2) == 1:
sum += i
i += 1
I expect sum to be 9 but an infinite loop occurs, causing the sum to be infinite.
I can't seem to understand how. Any help is appreciated. Thanks in advance!
You only increment i inside the if, so if the condition is not meant, i stays the same forever
i = 0
sum = 0
while i < 7:
if (i % 2) == 1:
sum += i
i += 1
print(sum)
Output:
9
As you keep conditioning i % 2, but if it doesn't go through, the i never changes, it will always exit the if statement, to solve it:
i = 0
sum = 0
while i < 7:
if (i % 2) == 1:
sum += i
i += 1
print(sum)
You have to unindent by 4 spaces your i += 1 line.
For similar cases it is better to use for loop than while loop. Especially if you have long loops. link
sum = 0
for i in range(7):
if (i % 2) == 1:
sum += i

Python Count up & Down loop

How can I simply transform this loop to count up from 1 to 100, and display the numbers? I'm starting to code recently. It works fine when counting down, but I can't figure out how to make it go from 1 -100
example:
count = 100
while count > 0 :
print(count)
count = count - 1
If you use a for loop it gets really easy:
for number in range(1,101):
print(number)
And for going from 100 down to 1:
for number in range(100,0,-1):
print(number)
Can try 'reversed':
>>> for i in reversed(range(1,11)):
... print i
...
10
9
8
7
6
5
4
3
2
1
Start at 1, and change your conditional to break out when you reach 100. Add 1 each loop through.
count = 1
while count <= 100:
print(count)
count += 1
just start your count at 1, change your check statement to check if the number is less than 100, and use "count = count + 1" Should work, good luck!
Basically just do the opposite of what you've already got.
count = 1
while count < 101:
print(count)
count = count + 1

print factors of a number in python

I'm trying to print the factors of the number 20 in python so it goes:
20
10
5
4
2
1
I realize this is a pretty straightforward question, but I just had a question about some specifics in my attempt. If I say:
def factors(n):
i = n
while i < 0:
if n % i == 0:
print(i)
i-= 1
When I do this it only prints out 20. I figure there's something wrong when I assign i=n and then decremented i, is it also affecting n? How does that work?
Also I realize this could probably be done with a for loop but when I use a for loop I can only figure out how to print the factors backwards so that I get: 1, 2, 5, 10....
Also I need to do this using just iteration. Help?
Note: This isn't a homework question I'm trying to relearn python on my own since it's been a while so I feel pretty silly being stuck on this question :(
while i < 0:
This will be false right from the start, since i starts off positive, presumably. You want:
while i > 0:
In words, you want to "start i off at n, and decrement it while it is still greater than 0, testing for factors at each step".
>>> def factors(n):
... i = n
... while i > 0: # <--
... if n % i == 0:
... print(i)
... i-= 1
...
>>> factors(20)
20
10
5
4
2
1
The while condition should be i > 0 and not i < 0 because it will never satisfy it as i begins in 20 (or more in other cases)
Hope my answer helps!
#The "while True" program allows Python to reject any string or characters
while True:
try:
num = int(input("Enter a number and I'll test it for a prime value: "))
except ValueError:
print("Sorry, I didn't get that.")
continue
else:
break
#The factor of any number contains 1 so 1 is in the list by default.
fact = [1]
#since y is 0 and the next possible factor is 2, x will start from 2.
#num % x allows Python to see if the number is divisible by x
for y in range(num):
x = y + 2
if num % x is 0:
fact.append(x)
#Lastly you can choose to print the list
print("The factors of %s are %s" % (num, fact))

Categories