Can someone help me figure out why this loop is infinite? The class I am in automatically inputs the variables for me per those last 2 lines. It passes the test with numbers 2 and 4. However there is another input, that I am unable to see, that keeps this running as an infinite loop. I can not figure out where there is a gap in this code that would allow an infinite loop. Any suggestions?
def shampoo_instructions(user_cycles):
N = 1
while N <= user_cycles:
if N < 1:
print('Too few')
elif N > 4:
print('Too many')
else:
print(N,': Lather and rinse.')
N = N + 1
print('Done.')
user_cycles = int(input())
shampoo_instructions(user_cycles)
Indent N = N + 1 out of the loop, otherwise it never get's to adding.
Or better use N += 1:
def shampoo_instructions(user_cycles):
N = 1
while N <= user_cycles:
if N < 1:
print('Too few')
elif N > 4:
print('Too many')
else:
print(N,': Lather and rinse.')
N = N + 1
print('Done.')
user_cycles = int(input())
shampoo_instructions(user_cycles)
First: get used to testing your code. Since you have conditionals involving the numbers 1 and 4, you should test numbers that are less than 1 and greater than 4 to see what happens beyond those edges. Sure enough, giving 5 as an input produces an infinite loop:
0
Done.
1
1 : Lather and rinse.
Done.
4
1 : Lather and rinse.
2 : Lather and rinse.
3 : Lather and rinse.
4 : Lather and rinse.
Done.
5
1 : Lather and rinse.
2 : Lather and rinse.
3 : Lather and rinse.
4 : Lather and rinse.
Too many
Too many
Too many
Too many
Too many
Too many
Why does that happen? user_cycles == 5 so the loop won't stop until N == 6 (or any value greater than 5.
What happens when N == 5? We print "Too many" and then continue the loop without incrementing N again. Hence the loop will always get stuck at N = 5.
Note that testing with these values also shows is that we never hit the Too few condition -- this is dead code! It's not possible to ever reach this condition because N always starts at 1 and is never decreased.
The way to fix the infinite loop depends on the desired behavior. You could break the loop as soon as N exceeds 4:
elif N > 4:
print('Too many')
break
Another option would be to make sure that N is always incremented, either by incrementing it inside that conditional block or incrementing it outside the entire if...elif...else statement rather than inside the else (which will only run for values between 1 and 4).
Related
I'm reading a book about python programming for begginers.
One of it's tasks is to write a prime number calcutator that calculates 'n' prime numbers.
So far I've studied strings, logic gates, while and conditions.
The idea is to make it using only those operators.
I need help because I'm stuck with this code.
Here's what I've done:
odd = 3
number = 2
limit = int(input('How many primes do you need: '))
remnant = number % odd
even_remnant = number % 2
counter = 0
while counter <= limit:
if number == 2:
print('2')
number += 2
elif (number % 2) != 0:
remnant = number % odd
while odd < number:
print('while2')
remnant = number % odd
if (number % odd) != 0 and odd == (number - 1):
print(f'{number}.')
odd = 3
number += 1
counter += 1
break
elif (number % odd) == 0:
break
odd += 2
elif (number % 2) == 0:
number += 1
odd = 3
What do you think?
Thanks everyone.
Put your debugging pants on, we're going in.
First, the code doesn't run as it's written. The variables counter and impar are undefined. First step is to remove syntax errors like that. Looks like we want to start counter at 0 and the line that uses impar isn't necessary so we can delete it.
odd = 3
number = 2
limit = int(input('How many primes do you need: '))
counter = 0
while counter < limit:
if number == 2:
print('2')
number += 2
elif (number % 2) != 0:
while odd < number:
print('while2')
remnant = number % odd
if (number % odd) != 0 and odd == (number - 1):
print(f'{number}.')
odd = 3
number += 1
break
elif (number % odd) == 0:
break
odd += 2
elif (number % 2) == 0:
number += 1
odd = 3
Now the code runs without error, but all it does is print
2
while2
And then fails to terminate.
So we know we enter the while odd < number loop only once and we don't print anything during that loop. If we also print the value of odd and number while we are in there we see odd = 3 and number = 5. Neither of the if conditions are met and the odd += 2 line is hit. Now odd = 5 and the while loop exits without printing 5 even though 5 is prime. If we want to hit our print statement by meeting the condition odd == (number - 1) we better go in steps of 1 when incrementing odd. Let's change to odd += 1 and re-run the code.
Now when I say I need 2 primes it prints
2
5
7
And then prints while2 forever. At least it prints prime numbers! But it skipped 3 and printed too many, and I had to use Ctrl-C to quit the program. Too many primes were printed because the outer loop while counter <= limit: condition was never reached. Inside the loop, we never increase the value of counter. Whenever we print a prime, we need to increase counter.
Also, to make sure we print 3, take a look at the first if condition in the loop.
if number == 2:
print('2')
number += 2 # Oops, we skipped over 3
Let's update this:
if number == 2:
print('2')
print('3')
counter += 2 # Let's count both of these!
number += 2
Also adding counter += 1 after the other print, re-running the code we get
How many primes do you need: 2
2
3
5.
How many primes do you need: 3
2
3
5.
7.
Oops, we are getting one more than we need. This is because when counter == limit we run the while loop one more time. Let's change our while loop condition to while counter < limit:. That change gets us just the right number of primes.
How many primes do you need: 4
2
3
5.
7.
But if we ask for 5
How many primes do you need: 5
2
3
5.
7.
And the program never exits. If we check the values of odd and number, we see that the loops is running with odd=3 and number=9 over and over again.
Reason through the code when odd=3 and number=9. We break out of the while odd < number while loop when we hit this code
elif (number % odd) == 0
break
But we never increase the value of number, so it is still equal to 9 the next time through the loop. Let's update this to
elif (number % odd) == 0
number += 1
break
Now when we re-run the code we get
How many primes do you need: 5
2
3
5.
7.
11.
Huzzah! And it works when asking for more primes as well. Here is the code as it is currently:
odd = 3
number = 2
limit = int(input('How many primes do you need: '))
counter = 0
while counter < limit:
if number == 2:
print('2')
print('3')
counter += 2
number += 2
elif (number % 2) != 0:
while odd < number:
if (number % odd) != 0 and odd == (number - 1):
print(f'{number}.')
odd = 3
number += 1
break
elif (number % odd) == 0:
number += 1
break
odd += 1
elif (number % 2) == 0:
number += 1
odd = 3
Now that we have working code, let's improve it! One of our bugs was that we forgot to increase number by 1 in one case. Notice that no matter how we exit the outer while loop while counter <= limit: we want to increment number. So, instead of doing it in many places, let's move all of those to the end of the while block.
We also set odd=3 whenever exiting the while block. What we want to ensure is that odd=3 at the start of the while loop, so let's move that there. Now there is no more code in the elif (number % 2) == 0: block, so we can remove that line.
number = 2
limit = int(input('How many primes do you need: '))
counter = 0
while counter < limit:
odd = 3
if number == 2:
print('2')
print('3')
counter += 2
elif (number % 2) != 0:
while odd < number:
if (number % odd) != 0 and odd == (number - 1):
print(f'{number}.')
counter += 1
break
elif (number % odd) == 0:
break
odd += 1
number += 1
I think the code is more clear if the while loop ends when the condition is met, rather than on break statements. We want the while loop to end if we find the number is divisible by something, or we run out of numbers to check.
`while number % odd != 0 and odd < number:`
And the only thing we need to do in the while loop is increment odd. Then after the loop, we can check the value of odd to see which condition was met.
number = 2
limit = int(input('How many primes do you need: '))
counter = 0
while counter < limit:
odd = 3
if number == 2:
print('2')
print('3')
counter += 2
elif (number % 2) != 0:
while number % odd != 0 and odd < number:
odd += 1
if odd == number: # No divisor was found!
print(f'{number}.')
counter += 1
number += 1
Notice that we are "hard coding" the divisibility by 2 (number % 2) != 0 and then using the variable odd to check divisibility by everything else. If we start odd at 2 instead of 3, we don't have to do the hard coding.
number = 2
limit = int(input('How many primes do you need: '))
counter = 0
while counter < limit:
odd = 2
if number == 2:
print('2')
print('3')
counter += 2
while number % odd != 0 and odd < number:
odd += 1
if odd == number: # No divisor was found!
print(f'{number}.')
counter += 1
number += 1
When we make this change, we also notice that we find the primes 2 and 3 twice, so we can remove the hard coded version of those:
number = 2
limit = int(input('How many primes do you need: '))
counter = 0
while counter < limit:
odd = 2
while number % odd != 0 and odd < number:
odd += 1
if odd == number: # No divisor was found!
print(f'{number}.')
counter += 1
number += 1
When I try to run this code it tells me that Counter is not set, so right before entering the loop set Counter to 0.
Another problem is that you start by finding 2 in the first case of your loop, this is nice. Here after the loop runs again, now with number set to 4. Because of your += 2 instruction.
It then runs the last elif case. where (number % 2) == 0. here it set number = 5, and odd = 3. But it doesn't print 3. I think you mean to do this.
Now it runs the loop again, and enter the second elif case (number % 2) != 0.
The first line in the elif clause the variable impar is not defined so it will fail.
I can't understand your program but it's good
def is_prime(n):
st = "prime" # being prime status
for i in range(2,n):
if n % i == 0: # if number is prime
st = "not prime"
break;
return st
n = int(input("enter n: "))
pc = 0 # prime conter
c = 1 # counter
while n != pc:
if is_prime(c) == "prime":
print (c)
pc += 1
c += 1
To calculate 'n' number for prime numbers you needn't use so many statements, if you make use of the the arithmetic and logical or bit-wise operators, which you will be learning in the future chapters of the python book you're referring.
I shall help you by editing the code for you.
number = int(input("Enter range: "))
print("Prime numbers:", end=' ')
for n in range(2, number):
for i in range(2, n):
if n % i == 0:
break
else:
print(n, end=' ')
Question:
Write a function print_shampoo_instructions() with parameter
num_cycles. If num_cycles is less than 1, print "Too few.". If more
than 4, print "Too many.". Else, print "N : Lather and rinse."
num_cycles times, where N is the cycle number, followed by "Done.".
Sample output with input: 2
1 : Lather and rinse.
2 : Lather and rinse.
Done.
My Error:
Program end never reached.
This is commonly due to an infinite loop or infinite recursion.
My code is as follows:
def shampoo_instructions(num_cycles):
if num_cycles < 1:
print ('Too few.')
elif num_cycles > 4:
print ('Too many.')
else:
i = 0
while i<num_cycles:
print (i+1,": Lather and rinse")
i = i + 1
print('Done')
shampoo_instructions(2)
user_cycles = int(input()) #cannot be altered per the program
print_shampoo_instructions(user_cycles) #cannot be altered per the program
Your actually reassigning i outside of the while loop. Inside the loop you're simply printing out i + 1. Instead reassign the counter inside the loop:
while i<num_cycles:
print (i+1,": Lather and rinse")
i = i + 1
def shampoo_instructions(user_cycles):
if user_cycles < 1:
print("Too few.")
elif user_cycles > 4:
print("Too many.")
else:
for i in range(1, user_cycles + 1):
print(i,":","Lather and rinse.")
print("Done.")
user_cycles = int(input())
shampoo_instructions(user_cycles)
In the sample program below, I tried to find the even number with the the number I typed in (2, 1, 4, 0) these 4 numbers. The result is 2, but I don't know what (n) means in this program.
a = int(input())
n = 0
while a != 0:
if a % 2 == 0:
n = n + 1
a = int(input())
print(n)
here n is used to count even numbers.
the loop will keep checking whether given input is 0 or not if it is 0 the loop will exit if not it will check whether it is even or odd, if the number is even it will increase the value of n by 1. If you input 10 even numbers the value of n will be 10 respectively.
n is your counter and probably you forgot to make a break from the loop the loop will be an infinite loop you have to add a condetion so that the loop stops like:
while n != 10 :
Or
add if statement
if n < 10 :
break #to quit the loop
This question already has answers here:
how to stop a for loop
(9 answers)
Closed 2 years ago.
As I am getting more and more comfortable with writing scripts in Python I wrote a script that finds prime numbers by trying to divide each number in a range in each number (kinda like Brute Force haha).
It works but I wanted to make the process faster and more efficient.
My code:
count = 0
for i in range(2,1000):
j = 2
while j < 1000:
if i%j==0:
count = count + 1
j = j + 1
if count == 1:
print(i)
count = 0
Explanation:
I check and store in count how many time does a number can be divided.
If by the end of the check count equals 1 that means that the number is prime (gets divided only by itself).
What I want now is to stop the process of checking how many times the number can get divided when the "count" variable exceed 1 (2 or more). How do I do that?
I wanted to do something with try and except but didn't know what...
Add an extra condition in here, and break out of the loop if the count is greater than one. Put this inside the while loop:
if i % j == 0:
count += 1
if count > 1:
break
As mentioned in the comments, it's cleaner to just use count as part of the loop condition. Here's an improved version:
for i in range(2, 1000):
j = 2
count = 0
while j <= i and count < 2:
if i % j == 0:
count += 1
j += 1
if count == 1:
print(i)
Of course, there are faster ways to find if a number is prime - in particular, in the inner loop you should not iterate until 1000, not even until i is reached, just until the sqrt(i), but I'll leave that as an improvement for you to make ;)
You can try using break. What break does is that it skips the remaining part of the loop and jumps to the statement following the loop.
count = 0
for i in range(2,1000):
j = 2
while j < 1000:
if i%j==0:
count = count + 1
break
j = j + 1 ### line 8
if count == 1:
print(i)
count = 0
In this code, when python encounters break, it skips the remaining part of the loop (that is, the remaining loop is not executed) and jumps straight to the next statement, which is line 8
Its all wrong, first you have to understand the algorithm to check for prime, basically a number is prime if it can't be fully divided by any number between 2 to (number//2)-1
Now in your question, you couldn't use break anywhere, bcz you first searching for all divisible and then checking prime, better I'm adding a sample code to your problem which is more feasible to find primes
Code
number = int(input("Enter A Number"))
for i in range(2,number//2+1): #See range arguments
if(number%i==0):
print("Not A Prime")
break #Break loop when condition reached
I've been working through a few exercises Python based to test my knowledge and I'm stuck on a task which I have got running but am not using the correct loops and statements.
My program needs to use two while loops and two continue statements to print odd numbers from 1 to 10. Then it will print even numbers in reverse from 8 down to 1.
I've gotten the odd to work and I'm using a step based range to do the even, not sure if there's a better way to do this?
But I'm unsure as to how I will include the two while loops and the two continue statements in my program for it to work and print into the console the same outcome as of now.
Furthermore, I'd like for my program to do include a break statement which will not print numbers higher than 6.
I'm just a bit confused on where to add this. I've been viewing many tutorials online but can't seem to find the right examples relevant.
All help and advice or where I can learn more knowledge is greatly appreciated!
Thank you for reading this!
for i in range(1,11):
if(i%2!=0):
print(i)
for i in range(8, 1, -2):
print(i)
This is one way to do it with two while loops and two continue statements:
n = 0
while n < 10:
n += 1
if n % 2 == 0:
continue
print(n)
while n > 1:
n -= 1
if n % 2 == 1:
continue
print(n)
This outputs:
1
3
5
7
9
8
6
4
2
And to avoid printing numbers higher than 6 by adding a break statement:
n = 0
while n < 10:
n += 1
if n % 2 == 0:
continue
if n > 6:
break
print(n)
while n > 1:
n -= 1
if n % 2 == 1:
continue
print(n)
This outputs:
1
3
5
6
4
2