The Gold, The and Cthulhu? (EX 35)

I typed in the code you gave in this example…

def gold_room
  puts "This room is full of gold. How much do you take?"
  
  print ">"
  choice = $stdin.gets.chomp
  
  #This line has a bug, so fix it
  if choice.include? ("0") || if choice.include? ("1")
    how_much = choice.to_i
  else
    dead("Man, learn how to type a number")
  end

  if how_much < 50
    puts "Nice, you're not greedy. YOU WIN!!!"
	exit(0)
  else
    dead("YOU GREEDY BASTARD!!")
  end
end


def bear_room
  puts "There is a bear here."
  puts "The bear has a bunch of honey."
  puts "The fat bear is in front of the door."
  puts "How are you going to move the bear?"
  bear_moved = false
  
  while true
    print ">"
	choice = $stdin.gets.chomp
	
	if choice == "take honey"
	  dead("The bear looks at you, then slaps your face off.")
	elsif choice == "taunt the bear" && !bear_moved
	  puts "The bear has moved from the door. You can go through now."
	  bear_moved = true
	elsif choice == "taunt the bear" && bear_moved
	  dead("The bear gets pissed off and chews your legs off!")
	elsif choice == "open door"  && bear_moved
	  gold_room
	else
	  puts "I got no idea what that means."
	end
  end
end


def cthulhu_room
  puts "Here you see the great evil known as Cthulhu."
  puts "He, it, whatever stares at you and you go insane!"
  puts "Do you flee for your life or eat your head?"
  
  print ">"
  choice = $stdin.gets.chomp
  
  if choice.include? "flee"
    start
  elsif choice.include? "head"
    dead("Well that was tasty!")
  else
    cthulhu_room
  end
end


def dead(why)
  puts why, "Good job!"
  exit(0)
end

def start
  puts "You are in a dark room."
  puts "there is a door to your right and left."
  puts "Which one do you take?"
  
  print ">"
  choice = $stdin.gets.chomp
  
  if choice == "left"
    bear_room
  elsif choice == "right"
    cthulhu_room
  else
    dead("You stumble around the room until you die of hunger.")
  end
end

start

and got these errors:

“ex35.rb:90: syntax error, unexpected end-of-input, expecting `end’”

“ex35.rb:90:in <main>': undefined local variable or methodstart’ for main:Object (NameError)” (added extra end)

Not too sure what happened.

You appear to be missing an end closing statement at line 20 (or probably line 13 is better style). Line 8 has two if statements that both needed to be ended (I totally forgot Ruby does this…)

The error pointing to line 90 is a bit misleading as this is where the failure occurs during execution, rather than the actual error point in the script. However the interpreter does tell you an ‘end’ is missing which does help a little. Always good to explore with the hints given. It can get kind of fun sometimes.

  1 def gold_room
  2   puts "This room is full of gold. How much do you take?"
  3
  4   print ">"
  5   choice = $stdin.gets.chomp
  6
  7   #This line has a bug, so fix it
  8   if choice.include? ("0") || if choice.include? ("1")
  9     how_much = choice.to_i
 10   else
 11     dead("Man, learn how to type a number")
 12   end
 13
 14   if how_much < 50
 15     puts "Nice, you're not greedy. YOU WIN!!!"
 16     exit(0)
 17   else
 18     dead("YOU GREEDY BASTARD!!")
 19   end
 20   end
 21 end
A free service run by Zed A. Shaw for learncodethehardway.org.