Python Count up & Down loop - python

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

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

How to make a program that infinitely sends a message after reaching a certain point

So I want to make a program that increases in 'count'. Every interval of 1 000 000, I want the program to say: "(1),000,000" marked has been reached. Of course I want that (1) to be replaced with 2, 3, 4, 5, 6 etc, whenever the interval has been reached. So when the two million mark has been reached I want it to say "2,000,000" mark has been reached."
Here is a simplified version of what my program is doing. However it is fixed to the intervals that I have set.
count = 0
while True:
count += 1
if count == 1000000:
print("The 1000000 mark has been reached.")
if count == 2000000:
print("The 2000000 mark has been reached.")
if count == 3000000:
print("The 3000000 mark has been reached.")
Try the following with a modulus operator:
count = 0
while True:
count += 1
if count % 1000000 == 0:
print(f"The {count} mark has been reached.")
For an explanation about the modulus (%) operator, see this article
You can use the modulo operator:
count = 0
while True:
count += 1
if count % 1000000 == 0:
print(f"The {count} mark has been reached.")
See if this one can help you:
Var, Count = 0, 1000000
while True:
Var += 1
#print(Var)
if Var == Count:
Count += 1000000
Var1 = str(Var).find("0")
Value = str(Var)[:Var1]+","+str(Var)[Var1::]
print(f"The {Value} mark has been reached")

How to run the python code with limit number in for loop?

I have a simple for loop code for calculate the sum of all multiples of 3 & 5 less than 100, and since I place the print under the if, it will shows all the sum of multiples of 3 & 5, and I want it only shows the sum of 3&5 before 100, what should I do? thanks.
range(1,100)
total = 0
for i in range(1, 100):
if i % 3 == 0 or i % 5 == 0:
total += i
print(total)
If I interpret your question correctly, you're asking how to only print the sum at the end, not at every point in the loop. The answer to this is to de-indent the print statement to be outside the if and for blocks:
range(1,100) # note: this line does nothing, you can remove it
total = 0
for i in range(1, 100):
if i % 3 == 0 or i % 5 == 0:
total += i
print(total)
You can compute this sum more simply with the sum function:
print(sum(
i for i in range(1, 100)
if i % 3 == 0 or i % 5 == 0
))
If what you are trying to ask is to print the total only once and not again and again, then the answer is simple, just unindent the line with the print statement, that is:
total = 0
for i in range(1, 100):
if i % 3 == 0 or i % 5 == 0:
total += i
print(total)
This is because, if the statement is indented, it will be treated as a part of the for loop and will run on every iteration of the for loop. Instead, unindent it to make it run only once and that is after the for loop is done running. The output for this code will be like this:

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

How do I exit this while loop?

I’m having trouble with exiting the following while loop. This is a simple program that prints hello if random value is greater than 5. The program runs fine once but when I try to run it again it goes into an infinite loop.
from random import *
seed()
a = randint(0,10)
b = randint(0,10)
c = randint(0,10)
count = 0
while True:
if a > 5 :
print ("aHello")
count = count + 1
else :
a = randint(0,10)
if b > 5 :
print ("bHello")
count = count + 1
else :
b = randint(0,10)
if c > 5 :
print ("cHello")
count = count + 1
else :
c = randint(0,10)
if count == 20 :
count = 0
break
count = 0
Your while loop might increment the variable count by 0, 1, 2 or 3. This might result in count going from a value below 20 to a value over 20.
For example, if count's value is 18 and the following happens:
a > 5, count += 1
b > 5, count += 1
c > 5, count += 1
After these operations, count's value would be 18 + 3 = 21, which is not 20. Therefore, the condition value == 20 will never be met.
To fix the error, you can either replace the line
if count == 20
with
if count >= 20
or just change your program logic inside the while loop.
Does the following code help?
while True:
if a > 5 :
print ("aHello")
count = count + 1
if count == 20 :
break
else :
a = randint(0,10)
if b > 5 :
print ("bHello")
count = count + 1
if count == 20 :
break
else :
b = randint(0,10)
if c > 5 :
print ("cHello")
count = count + 1
if count == 20 :
break
else :
c = randint(0,10)
You have to check the value of count after incrementing it every time.
The "break" condition might fail if two or more values of the variables a, b, and c are greater than 5. In that case the count will be incremented more than once and count will end up > 20, and the loop can never terminate. You should change:
if count == 20 :
to
if count >= 20:
At the end of iteration, count might be greater than 20 due to multiple increments. So I would update the last if statement to:
if count >= 20:
to feel safe.
If your goal is to stop counting when count is >= 20, then you should use that condition for your while loop and you won't need to break at all, as you only break at the end of the loop anyways.
The new while statement would look like
while count < 20:
# increment count
and then outside of the while loop you can reset count to 0 if you want to use it again for something else.
since you increment count 2 or 3 times in one iteration, it may skip past your count == 20 check
Here's one way to get exactly 20 lines.
from random import seed, randint
seed()
a = randint(0,10)
b = randint(0,10)
c = randint(0,10)
count = iter(range(20))
while True:
try:
if a > 5:
next(count)
print ("aHello")
else:
a = randint(0,10)
if b > 5:
next(count)
print ("bHello")
else:
b = randint(0,10)
if c > 5:
next(count)
print ("cHello")
else:
c = randint(0,10)
except StopIteration:
break
Note there is still a lot of repetition in this code. Storing your a,b,c variables in a list instead of as separate variables would allow the code to be simplified further

Categories