Refactor factory 3/17 -Omit needless words


#1




Hi. I keep getting this error code:
"Oops, try again. It looks like the first_n_primes method no longer returns 'n must be greater than 0.' for negative input. "

I don't know what I'm doing wrong as I believe 2-3 lines of code are correct.

Thanks


$VERBOSE = nil    # We'll explain this at the end of the lesson.
require 'prime'   # This is a module. We'll cover these soon!

def first_n_primes(n)

return "n must be an integer." unless n.is_a? Integer

return "n must be greater 0." if n <= 0

  
prime_array ||= [] 
  
  prime = Prime.new
  for num in (1..n)
    prime_array.push(prime.next)
  end
  return prime_array
end

first_n_primes(10)


#2

I think these lines need to be indented like this:

$VERBOSE = nil    # We'll explain this at the end of the lesson.
require 'prime'   # This is a module. We'll cover these soon!

def first_n_primes(n)

  return "n must be an integer." unless n.is_a? Integer

  return "n must be greater than 0." if n <= 0
  
  prime_array ||= []
  
  prime = Prime.new
  for num in (1..n)
    prime_array.push(prime.next)
  end
  return prime_array
end

first_n_primes(10)

#3

Thanks for the reply.

I just tried and it didn't work. :frowning:


#4

Did you copy the exact code I posted?
If you did: refresh the page or load a new browser.
If you did not: try that because the indentation is a bit weird.


#5

I copied your code and worked! Thank you!

Though a little strange why mere indentation would cause an error...


#6

If you've ever messed with python, indentation is a HUGE part of the code.
I don't know specifically how the computer interprets it, but it's something that can cause a lot of issues.

Glad you got it to work. :slight_smile:


#7

You don't have to type return, the program will run without it. (Omit needles words)


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.