Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Operators and Control Structures Logical Operators The And (&&) Operator

Use of elsif operator

Please help me with this code, I have revised it but it doesn work:

def check_speed(car_speed) print "Enter car speed " carspeed = gets.chomp.to_i if (car_speed >= 40) && (carspeed <= 50) puts "safe" elsif puts "unsafe"
end

Thanks for your help!

ruby.rb
def check_speed(car_speed)
  print "Enter car speed "
  carspeed = gets.chomp.to_i
  if (car_speed >= 40) && (carspeed <= 50)
    puts "safe"
    elsif
    puts "unsafe"  
end

2 Answers

Hi Leonardo,

You're pretty much there with that - just a couple of points.

Firstly, you don't need to get user input with gets as the value is passed in as an argument. Second; your second test of car_speed has missed the underscore; correct that. Next, the strings need to be returned, not output, so change puts to return (or just leave it out). Lastly, you're missing an end and you don't need an elsif as there's no second conditional test; just use else.

You end up with:

def check_speed(car_speed)
  if car_speed >= 40 && car_speed <= 50
    return "safe"
  else
    return "unsafe"
  end
end

I hope that helps.

Steve.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Leonardo,

For the most part, the code is correct, but you are adding way more than what the challenge is asking for. Also, there are a couple syntax errors.

First, the challenge doesn't ask for you to "prompt" for a speed or create a variable for it, so those lines need to be deleted. Second, the challenge wants you to return the strings, and you are using puts.

For the syntax errors, the first one is you are using elsif (which is just misused rather than syntax error). The challenge needs you to use else. Second, there is a missing end to close out the function.

Once corrected:

def check_speed(car_speed)
  if(car_speed >= 40) && (car_speed <= 50)
    return "safe"
  else
    return "unsafe"
  end
end

Don't forget with challenges, they are often very strict and very picky. If you add code not asked for, delete code that was pre-loaded, or use the wrong word, etc, the challenge (most times) returns "Bummer".

Hope this helps and makes sense. Keep Coding! :)

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Edit: I originally stated elsif was misspelled, but I was thinking of Python syntax (elif). The above answer has been corrected to no longer state this.

Haha! Two pretty much identical answers! You got there first, though!! :-)