What is the fault of this program?

A forum for general discussion of the Python programming language.

What is the fault of this program?

Postby b849961@drdrb.com » Thu Oct 24, 2013 12:47 pm

I created this program to display prime numbers between 1 to 10. But there have fault. Please help me.

Code: Select all
A = 2
while (A <= 10) :
    Isprime = 1
    B =2
    while (B < A) :
        if (A % B == 0) :
            Isprime = 0
            B = B+1
        else : B = B+1
    else :
        if (Isprime != 0) :
            print(A)
        else : A = A+1
Last edited by stranac on Thu Oct 24, 2013 12:48 pm, edited 1 time in total.
Reason: First post lock.
My Python Version is 3.2.3
b849961@drdrb.com
 
Posts: 13
Joined: Thu Oct 24, 2013 12:41 pm

Re: What is the fault of this program?

Postby stranac » Thu Oct 24, 2013 12:49 pm

b849961@drdrb.com wrote:But there have fault. Please help me.

What's the problem?
If you're getting an error traceback, paste it, if not explain what's not working as you wanted.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: What is the fault of this program?

Postby b849961@drdrb.com » Thu Oct 24, 2013 12:59 pm

stranac wrote:
b849961@drdrb.com wrote:But there have fault. Please help me.

What's the problem?
If you're getting an error traceback, paste it, if not explain what's not working as you wanted.

The error is displaying number 2 infinite time. Please help me.
Untitled.jpg
Untitled.jpg (72.23 KiB) Viewed 389 times
My Python Version is 3.2.3
b849961@drdrb.com
 
Posts: 13
Joined: Thu Oct 24, 2013 12:41 pm

Re: What is the fault of this program?

Postby Kebap » Thu Oct 24, 2013 1:27 pm

Hi b849961@drdrb.com, welcome to the forums! Your code basically comes down to this:

Code: Select all
A = 2
while (A <= 10) : # A will never be increased -> endless loop
    Isprime = 1
    B =2
    while (B < A) : # never true, skip this block
        pass
    else :
        if (Isprime != 0) : # always true
            print(A) # Here the number 2 is printed
        else : A = A+1 # this will never be reached
# and then it begins again. 2, 2, 2, 2, 2, etc.


I am not sure what you intended to do, and what went wrong, but this explains why it is happening like that.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 403
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: What is the fault of this program?

Postby b849961@drdrb.com » Thu Oct 24, 2013 2:41 pm

Kebap wrote:Hi b849961@drdrb.com, welcome to the forums! Your code basically comes down to this:

Code: Select all
A = 2
while (A <= 10) : # A will never be increased -> endless loop
    Isprime = 1
    B =2
    while (B < A) : # never true, skip this block
        pass
    else :
        if (Isprime != 0) : # always true
            print(A) # Here the number 2 is printed
        else : A = A+1 # this will never be reached
# and then it begins again. 2, 2, 2, 2, 2, etc.


I am not sure what you intended to do, and what went wrong, but this explains why it is happening like that.

Thank you Kebap. Your explaining is succeed my program. Thank you. If you want code here that code.

Code: Select all
A = 2
while (A <= 10) :
    Isprime = 1
    B =2
    while (B < A) :
        if (A % B == 0) :
            Isprime = 0
            B = B+1
        else : B = B+1
    else :
        if (Isprime != 0) :
            print(A)
            A = A+1
        else : A = A+1
My Python Version is 3.2.3
b849961@drdrb.com
 
Posts: 13
Joined: Thu Oct 24, 2013 12:41 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests