Code Help!!!!!

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

Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 6:30 pm

def getPentagonalNumber(i):

pentNumber = (((i) * ((3 * i) - 1)) // 2)
print(str("pentagonalNumber"),end = " ")

def main():
for i in range(1,101):
getPentagonalNumber(i)
if i % 10 != 0:
print(format(getPentagonalNumber(i), "6d"),end = "")



I keep getting the"PentagonalNUmber" over and over again. Not sure what I have wrong here. :?:
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby Yoriz » Sun Apr 14, 2013 6:42 pm

Hi, please could you read this and then alter you code to use code tags so the code is indented correctly, indenting is important in python.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 6:54 pm

Code: Select all
def getPentagonalNumber(i):
   
   pentNumber = (((i) * ((3 * i) - 1)) // 2)
   print(str("pentagonalNumber"),end = " ")

def main():   
   for i in range(1,101):
     getPentagonalNumber(i)
   if i % 10 != 0:
         print(format(getPentagonalNumber(i), "6d"),end = "")
   
   else:
   
      return()
   
main()
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby Yoriz » Sun Apr 14, 2013 7:04 pm

Each time you do
Code: Select all
getPentagonalNumber(value)

the line
Code: Select all
print(str("pentagonalNumber"), end=" ")

is being told to print "PentagonalNUmber"

You call it twice inside your loop, i think you probably want getPentagonalNumber to return a value rather then print it, then only call it once for each loop and use the value it returns.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 7:33 pm

Code: Select all
def getPentagonalNumber(i):
 
   pentagonalNumber = (((i) * ((3 * i) - 1)) // 2)
   print(str(pentagonalNumber), end = " ")

for i in range (1,101):
      getPentagonalNumber(i)
      if i % 10 != 0:
     
print()



I am still getting an incorrect result and a syntax error on line 16
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby Yoriz » Sun Apr 14, 2013 7:46 pm

The code you posted last doesn't even have 16 lines.
Your still printing inside getPentagonalNumber, i didn't think you wanted it to print.
Your still not returning a value from getPentagonalNumber.
Your if statement has no code after it.
when you post an error give the traceback as asked for in the link i asked you to read in my first post here it is again.
http://www.python-forum.org/viewtopic.php?f=6&t=145
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 8:23 pm

Code: Select all
def getPentagonalNumber(i):
 
   pentagonalNumber = (((i) * ((3 * i) - 1)) // 2)
   
for i in range (1,101):
    getPentagonalNumber(i)
    if i % 10 != 0:
        print(format(getPentagonalNumber(i), "6d"))
    else:
        print(format(getPentagonalNumber(i), "6d"), end = " ")
    return pentagonalNumber


return pentagonalNumber
^
SyntaxError: 'return' outside function


This was a code I originally had earlier today with a return. I think my "return" is in the wrong spot.
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby Yoriz » Sun Apr 14, 2013 8:57 pm

Code: Select all
def getPentagonalNumber(i):
    return (((i) * ((3 * i) - 1)) // 2)  # this is the part i keep saying needs
                                         # to return the value

for i in range(1, 101):
    pentNum = getPentagonalNumber(i)  # get the return value here
    if i % 10 != 0:
        print(format(pentNum, "6d"))  # use the returned value here
    else:
        print(format(pentNum, "6d"), end=" ")  # use the returned value here
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 9:11 pm

Oh, ok Sorry I was not reading very carefully I guess. Thank You!!
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 9:19 pm

Any chance you could help me with another code. I need to find Twin prime Numbers up to 1000. I have worked out 2 different codes and neither are working.
code 1:
Code: Select all
def isPrime(number):
   
   divisor = 2
   while divisor <= number / 2:
      if number % divisor == 0:
         return False # number is not a prime
      divisor += 1
                return True # number is prime
   

for i in range (1, 1001):
   if isPrime(number):
      value = number + 1
      print("(",number,",", value,")", end = ")
      if count % NUMBER_OF_PRIMES_PER_LINE == 0:
         print()
   number += 1

def main():
   for i in range(1000):
      if isPrime(i):
         if isPrime(i + 2):
            print('(' + str(i) + ',',str(i + 2) + ')', end = ")
                                print()

main()


False # number is not a prime
^
TabError: inconsistent use of tabs and spaces in indentation


Incorrect result!



code 2:
Code: Select all
ef isPrime(number):
    divisor = 2
    while divisor <= number / 2:
        if number % divisor == 0:
            return False # number is not a prime
        divisor += 1
        return True # number is a prime
   
   
       
 
def printTwinPrimeNumbers(twinPrime):
    NUMBER_OF_PRIMES = 50
    NUMBER_OF_PRIMES_PER_LINE = 10
    number = 1 # number to be tested for primess
    count = 0   
   
   
    for i in range(1, 1001):
        if isPrime(number):
            value = number + 1
            print("(", number,",", value,")", end = '')
            if count % NUMBER_OF_PRIMES_PER_LINE == 0:
                print()
               
               
        number += 1
           
def main():
    print("The first 50 twin prime numbers are: ")
    printTwinPrimeNumbers(50)
   
main()


i get an error on line 24 stating that 'number' is not defined
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby Yoriz » Sun Apr 14, 2013 9:26 pm

Error one you need to correct the indentation only use 4spaces for a tab not a real tab

Error two you are trying to use a variable called number before you have defined it
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 9:49 pm

Code: Select all
def isPrime(number):
   
    divisor = 2
    while divisor <= number / 2:
        if number % divisor == 0:
            return False # number is not a prime
        divisor += 1
        return True # number is prime
   

for i in range (1, 1001):
    if isPrime(number):
        value = number + 1
        print("(",number,",", value,")", end = "")
    if count % NUMBER_OF_PRIMES_PER_LINE == 0:
            print()
    number += 1

def main():
    for i in range(1000):
        if isPrime(i):
   if isPrime(i + 2):
           print('(' + str(i) + ',',str(i + 2) + ')', end = ")
                print()

main()



Still getting
if isPrime(i + 2):
^
TabError: inconsistent use of tabs and spaces in indentation
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby Yoriz » Sun Apr 14, 2013 9:59 pm

Code: Select all
def main():
    for i in range(1000):
        if isPrime(i):
   if isPrime(i + 2):
           print('(' + str(i) + ',',str(i + 2) + ')', end = ")
                print()


The error is telling you what is wrong.
Code: Select all
if isPrime(i + 2):
^
TabError: inconsistent use of tabs and spaces in indentation

The indentation is wrong on
Code: Select all
   if isPrime(i + 2):

look at the line before it , it is indented 8 spaces then you next line is only 3 spaces as the previous was an 'if' then next line should be indented 12 spaces.

Note when you've corrected that one look at the indentation for the lines that follow it
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 10:39 pm

This is going to sound stupid but to define "number" do I literally write 'def number'?
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby Yoriz » Sun Apr 14, 2013 10:51 pm

when you do
Code: Select all
def number():

You are creating a function called number and that means number is defined
or you might want number to be a variable then you would do
Code: Select all
number = 1

now number is defined as an int of 1

have you done any tutorials yet ?
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Code Help!!!!!

Postby lmp725 » Sun Apr 14, 2013 11:32 pm

Code: Select all
def isPrime(number):

    def number():   
        divisor = 2
        while divisor <= number / 2:
            if number % divisor == 0:
            return False # number is not a prime
            divisor += 1           
            return True # number is prime
   

for i in range (1, 1001):
    if isPrime(number):
        value = number + 1
        print("(",number,",", value,")", end = "")
    if count % NUMBER_OF_PRIMES_PER_LINE == 0:
            print()
    number += 1

def main():
    for i in range(1000):
        if isPrime(i):
            if isPrime(i + 2):
                print('(' + str(i) + ',',str(i + 2) + ')', end = "")
                print()

main()


return False # number is not a prime
^
IndentationError: expected an indented block



still getting that same error. I have done a small tutorial, I have also been watching the MIT open source Intro to Computer Science on iTunesU. This is for a 8 week class through a college that has the worst text book ever. Nothing is explained so you do not know how to take examples and convert it to work with the problems. It also does not show how to tie everything together.
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby Yoriz » Sun Apr 14, 2013 11:39 pm

Here is a tutorial on this forum about indentation
http://www.python-forum.org/viewtopic.php?f=25&t=209&p=378&hilit=indent#p378

I think it would be a good idea for you to go through the python tutorial
http://docs.python.org/3/tutorial/index.html for python 3
http://docs.python.org/2.7/tutorial/index.html for python 2

Your error is that you have not indented after the line
Code: Select all
if number % divisor == 0:
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Code Help!!!!!

Postby lmp725 » Mon Apr 15, 2013 12:34 am

Code: Select all
def isPrime(number):
    divisor = 2
    while divisor <= number / 2:
        if number % divisor == 0:
           
            return False # number is not a prime
        divisor += 1

    return True # number is prime

def printTwinPrimeNumbers(twinPrime):
    NUMBER_OF_PRIMES = 50
    number = 2 # number to be tested for primeness

    for i in range(1, 1001):
        if isPrime(number):
            value = number + 2
            print("(", number, ",", value, ")", end = '')
           
        number += 1

def main():
    print("The Twin Prime Numbers are: ")
    printTwinPrimeNumbers(50)

main()




Ok so this is my code from yesterday the only issue is I am not getting prime numbers rather I am getting even numbers for the first set. I have no idea what I am doing wrong to get even numbers for the first set.
lmp725
 
Posts: 13
Joined: Sun Apr 14, 2013 6:27 pm

Re: Code Help!!!!!

Postby stranac » Mon Apr 15, 2013 10:50 am

You're not checking if the second number of the pair is prime.
Friendship is magic!

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


Return to General Coding Help

Who is online

Users browsing this forum: Mekire, Yoriz and 4 guests