Debugging question

This is the place for queries that don't fit in any of the other categories.

Debugging question

Postby BruceWayne » Tue Aug 05, 2014 12:28 pm

So I have the following program:

Code: Select all
def is_prime(n):
    k = 3

    if (n == 2):
       flag = true
    elif (n % 2 == 1):
       flag = false
    else:
       while (k*k < n):
          if (n % k == 0):
             flag = false
             break

    return flag

n = int(input('Please enter positive integer: '))

for i in range(n):
    if (is_prime(i)):
       print(i)


The error I'm getting is the following:

Code: Select all
Traceback (most recent call last):
   line 22, in <module>
    if (is_prime(i)):
   line 8, in is_prime
    flag = false
NameError: name 'false' is not defined





I'm new to python and have no idea how to debug the code. Can anyone help me out please?
Last edited by metulburr on Tue Aug 05, 2014 12:36 pm, edited 1 time in total.
Reason: locked added code tags
BruceWayne
 
Posts: 8
Joined: Tue Aug 05, 2014 12:13 pm

Re: Debugging question

Postby metulburr » Tue Aug 05, 2014 12:35 pm

please read New users link in my signature. Please use code tags next time.

true and false in python are capitalized
Code: Select all
False

Code: Select all
True
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1472
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Debugging question

Postby BruceWayne » Tue Aug 05, 2014 12:42 pm

Thanks Ill keep that in mind for next post.

However, there is something wrong with the maths of the code and am not sure what it is. The prime numbers aren't being printed.
BruceWayne
 
Posts: 8
Joined: Tue Aug 05, 2014 12:13 pm

Debug Question 1.1

Postby BruceWayne » Tue Aug 05, 2014 1:08 pm

So this is what I have at the moment. This program should read in a positive integer, and print out all
the positive primes less than this integer. However, it doesn't print the prime numbers. I think there's an issue in the maths of the code, but again I'm new to python and got thrown this code to debug and am absolutely stuck. Any help would be appreciated.

Code: Select all
def is_prime(n):
    k = 3

    if (n == 2):
       flag = True
       return flag
    elif (n % 2 == 1):
       flag = False
       return flag
    else:
       while (k*k < n):
          if (n % k == 0):
             flag = False
             return flag
             break


n = int(input('Please enter positive integer: '))

for i in range(n):
    if (is_prime(i)):
       print(i)
Last edited by metulburr on Tue Aug 05, 2014 1:29 pm, edited 1 time in total.
Reason: merged newly created topic into previous same topic
BruceWayne
 
Posts: 8
Joined: Tue Aug 05, 2014 12:13 pm

Re: Debugging question

Postby Larz60+ » Tue Aug 05, 2014 2:16 pm

Hello,

If you write it like so:

Code: Select all
def is_prime(n):
  k = 3

  if n == 2:
    print ("1")
    flag = True
  elif n % 2 == 1:
    print ("2")
    flag = False
  else:
    print ("3")
    while k*k < n:
      print ("4")
      if n % k == 0:
        print ("5")
        flag = False
        break
     
    return flag

n = int(input('Please enter positive integer: '))

for i in range(n):
  if (is_prime(i)):
    print(i)


with an input of 5, It will print 3, however the while loop is satisfied before ever executing (no 4), therefore your flag never gets set, and thus an error.

Note that I removed the parenthesis around your conditions, otherwise the logic changes

Larz60+
Larz60+
 
Posts: 208
Joined: Thu Apr 03, 2014 4:06 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests