Learn Ruby HARD Way Упражнение 35 - PullRequest
0 голосов
/ 29 февраля 2020

Итак, я попытался выполнить это упражнение, чтобы заставить его работать и хорошо, убедитесь сами ...

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

и получил эти ошибки:

“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)

Я не уверен, что случилось.

1 Ответ

2 голосов
/ 29 февраля 2020

В вашем коде слишком много if. Вам нужен только один.

замените

 if choice.include?("0") || if choice.include?("1")

на

 if choice.include?("0") || choice.include?("1")

или более красивым

if %w[0 1].include?(choice)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...