Reputation: 39
I get these errors :
36.rb:45: syntax error, unexpected tIDENTIFIER, expecting keyword_end
if door2 == "yes" || door2 == "Yes"
^
ex36.rb:45: syntax error, unexpected tCONSTANT, expecting keyword_end
if door2 == "yes" || door2 == "Yes"
^
ex36.rb:47: syntax error, unexpected tIDENTIFIER, expecting keyword_end
elsif door2 == "no" || door2 == "No"
^
ex36.rb:47: syntax error, unexpected tCONSTANT, expecting keyword_end
elsif door2 == "no" || door2 == "No"
^
ex36.rb:48: syntax error, unexpected tCONSTANT, expecting keyword_end
puts "Well, you tried."
^
ex36.rb:48: dynamic constant assignment
puts "Well, you tried."
^
ex36.rb:48: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
puts "Well, you tried."
^
ex36.rb:49: syntax error, unexpected tCONSTANT, expecting keyword_end
puts "You are now dead of thirst. Good night!"
^
ex36.rb:51: syntax error, unexpected tCONSTANT, expecting keyword_end
puts "I don\'t understand you, sorry."
^
ex36.rb:51: unterminated string meets end of file
when running this code :
def start
puts "There is two doors for you to choose."
puts "Do you pick door number one or door number two?"
print ">> "
door = $stdin.gets.chomp
if door == 'one' || door == '1'
door_one
elsif door == 'two' || door == '2'
door_two
else
puts "A dark mist is sprayed into the air."
puts "Your body starts swelling up."
puts "You explode and are now dead. Good job!"
puts "\nWould you like to restart?"
print ">> "
restart = $stdin.gets.chomp
if restart == "yes" || restart == "Yes"
puts "OK\n"
start
else
puts "OK, have a nice day."
end
end
end
def door_one
puts "You see 5 bee hives with bees swarming around them. What do you do?"
print ">> "
choice = $stdin.gets.chomp
if choice == "Attack!"
puts "You suffer from 500 bee stings."
puts "Due to a combination of the venom, the stress your body is taking and your severe bee allergy, you die."
elsif choice == "Go back through the door"
puts "The door is locked and the bees attack you. You\'re dead now. Good night!"
elsif choice == "Befriend them" || choice == "Befriend the bees"
puts "The bees let you pass."
puts "You now see another door."
puts "Do you go through it?
print ">> "
door2 = $stdin.gets.chomp
if door2 == "yes" || door2 == "Yes"
door_two
elsif door2 == "no" || door2 == "No"
puts "Well, you tried."
puts "You are now dead of thirst. Good night!"
else
puts "I don\'t understand you, sorry."
door_one
end
else
puts "You confuse me."
door_one
end
end
def door_two
puts "You are now standing in front of a giant abyss. What do you do?"
print ">> "
choice = $stdin.gets.chomp
if choice == "Jump in." || choice == "Jump" || choice == "Jump." || choice == "Jump in"
puts "You jump in, after a while you start seeing light."
puts "You start feeling very sleepy and decide you are going to take a nap."
puts "You never wake up. Good night!"
elsif choice == "Sit" || choice == "Sit down" || choice == "Sit." || choice == "Sit down."
puts "Your body starts to rot, Your bones can no longer move."
puts "You have been paralyzed."
puts "You are dead."
else
puts "You confuse me."
door_two
end
end
start
Any help would be appreciated, I know that is a lot of errors but I have been trying to figure them out for days.
I am currently writing Exercise 36 of Learn Ruby The Hard Way and I really want to move on to Learn C The Hard Way or Eloquent Ruby
Thanks in advance
Upvotes: 0
Views: 468
Reputation: 84343
if door2 == "yes" || door2 == "Yes"
While ||
and or
have roughly equivalent semantics, they have a different precedence. You either need to separate your expressions with parentheses, or use the lower-precence or
operator instead. For example:
# Separate your expressions.
if (door2 == "yes") || (door2 == "Yes")
# Use a lower-precedence operator.
if door2 == "yes" or door2 == "Yes"
You'll need to do this throughout your codebase.
More idiomatically, you can simply search for a case-insentive regular expression anchored to the start of your response string. While it doesn't really address your problem directly, it neatly sidesteps the issue of operator precedence in your current if-statement while also providing a bit more flexibility. For example:
if door2 =~ /\Ay(?:es)?/i
Upvotes: 2