我收到这些错误:
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
运行此代码时:
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
任何帮助都会受到赞赏,我知道这是一个很多错误,但我一直试图弄清楚它们。
我目前正在编写“学习Ruby The Hard Way”的练习36,我真的想继续学习C The Hard Way或Eloquent Ruby
提前致谢
答案 0 :(得分:2)
如果door2 =="是" || door2 =="是"
虽然||
和or
具有大致相同的语义,但它们具有不同的优先级。您需要使用括号分隔表达式,或使用低级先验or
运算符。例如:
# Separate your expressions.
if (door2 == "yes") || (door2 == "Yes")
# Use a lower-precedence operator.
if door2 == "yes" or door2 == "Yes"
您需要在整个代码库中执行此操作。
更惯用的是,您只需搜索一个固定在响应字符串开头的case-insentive正则表达式。虽然它并没有直接解决您的问题,但它在您当前的if语句中巧妙地回避了运算符优先级的问题,同时还提供了更多的灵活性。例如:
if door2 =~ /\Ay(?:es)?/i