0

I have been playing around on CodeCademy with some Ruby since yesterday, and I feel as though I am grasping it slightly, but a few issues are becoming visible.

What is wrong with this code? I keep getting an error:

(ruby):15: syntax error, unexpected $end, expecting keyword_end

print "What grade did you get on your final?"
grade = gets.chomp
if grade > 1
    puts "You're just making that up! Really?"
answer = gets.chomp
yes = true
no = false

if answer == true
    print "Oh, that's awesome!"
if answer == false
    print "Dang, I thought you were serious."
else
    print "What? I don't understand."
end
1
  • Welcome to Stack Overflow, please make sure you visit the tour page and the How to Ask page. Commented Jul 21, 2015 at 13:35

3 Answers 3

2

It becomes clear with proper indentation

print "What grade did you get on your final?"
grade = gets.chomp
if grade > 1
  puts "You're just making that up! Really?"
  answer = gets.chomp
  yes = true
  no = false

  if answer == true
    print "Oh, that's awesome!"
    if answer == false
      print "Dang, I thought you were serious."
    else
      print "What? I don't understand."
    end

Every if in Ruby needs a corresponding end. You have three ifs and only one end. So only the last if answer == false has a matching end, you need two more for the others.

Sign up to request clarification or add additional context in comments.

Comments

0

You have two if statements that do not have a corresponding end.

if grade > 1
  puts "You're just making that up! Really?"
end

and

if answer == true
  print "Oh, that's awesome!"
end

On an unrelated note, I believe you want puts instead of print.

Also, when you have a boolean value, you do not need to do

if my_boolean == true
  # do something
end

It is the exact same thing as doing

if my_boolean
  # do something
end

And lastly, boolean values only have two possible values, true and false. That last else statement you have does not make sense, because it will be activated whenever answer == true, which you already have a case for.

2 Comments

I got it to work by doing this: pastebin.com/raw.php?i=pFZpG4qv (Paste of the code) but still not sure how to make the else statement work. I want it to say "What? I don't understand." if the user puts anything other than yes or no. I considered the || function, but not sure how that would work properly.
You want to compare with what the user said. answer == 'yes' then you can do an elsif answer == 'no', then you can use your else statement.
0

You need an end to each if that you have. You can do something like this:

puts "What grade did you get on your final?"
grade = gets.chomp

if grade > 1
  puts "You're just making that up! Really?"
end

answer = gets.chomp

if answer == "yes"
  print "Oh, that's awesome!"
elsif answer == "no"
  print "Dang, I thought you were serious."
else
  print "What? I don't understand."
end

or can be like this, where you don´t need to use the end

puts "What grade did you get on your final?"
grade = gets.chomp

# HERE
puts "You're just making that up! Really?" if grade > 1

answer = gets.chomp

if answer == "yes"
  print "Oh, that's awesome!"
elsif answer == "no"
  print "Dang, I thought you were serious."
else
  print "What? I don't understand."
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.