2016-05-27 48 views
-4

这是我到目前为止有:选择自己的冒险 - 语法错误,意外结束输入,期待keyword_end

puts "It's 9:05am and you just woke up to the sound of your alarm. You had originally set your alarm for 8:30am. 'good job' you think to yourself as you roll out of bed" 
puts "you are already running 35 minutes late for work. Do you take a shower?" 

print ">" 
shower = $stdin.gets.chomp 

if shower = "yes" or "sure" or "ok" 
    puts "Good call, you kind of smell from having a sweaty nighmare of a dream." 
    puts " You have a quick five minute shower and it is 9:10am. " 
     puts "You work in 50 minutes.Do you: " 
     puts " Take another 5 minutes to goof off?" 
     puts " decide to goof off later" 

     print "> " 
     jack = $stdin.gets.chomp 

    if jack == "goof off" 
     puts "you goofball" 
      puts "you have work in 45 minutes. Do you:" 
      puts "1. eat breakfast " 
      puts "2. take it with you" 

      print ">" 
      breakfast = $stdin.gets.chomp 

      if breakfast == "1" 
      puts "You notice all the knives in your house are missing and you " 
      puts "wanted to cut up toast. " 
      puts "This is unlucky but you use a spoon." 
      elsif breakfast == "2" 
      puts "you grab a kop tart, a knock off brand of pop tart you found at" 
      puts "Costco for $1 less" 
      else 
      puts "Well, doing that's prob better in the morning anyways" 
      end 

    elsif jack == "later" or "goof off later" 
     puts "your call"  
      puts "you have work in 50 minutes. Do you:" 
      puts "1. eat breakfast " 
      puts "2. take it with you" 

      print ">" 
      poptart = $stdin.gets.chomp 

      if poptart == "1" 
      puts "You notice all the knives in your house are missing and you " 
      puts "wanted to cut up toast. " 
      puts "This is unlucky but you use a spoon." 
     elsif poptart == "2" 
      puts "you grab a kop tart, a knock off brand of pop tart you found at" 
      puts "Costco for $1 less" 
     else 
      puts "Well, doing that's prob better in the morning anyways" 
      end 


else shower == "no" or "not sure" or "nah" 
    puts "that is messed up, because you smell. but yolo time to leave for work." 
    puts "on the way to work you wonder why you had such a strange dream." 
    puts "you begin to remember parts of it, an old house, the number 205..." 
     puts "on the walk to work you see an old house marked 205, do you: " 
     puts "Go inside the house, it looks abandoned and familliar" 
     puts "keep walking to work" 

     print ">" 
     old_house = $stdin.gets.chomp 

     if old_house == "go inside" or "go in" or "go into the house" or "go" 
     puts "you open the door and realize it was a huge coincidence and also not empty" 
     elsif olf_house == "keep walking" or "walk" or "go to work" 
     puts "you walk to work, and you get fired for being late." 
     puts "you walk back to the old house" 
     else 
     puts "oh well, you fail" 
     end 

我得到一个错误,指出:语法错误,意外的输入结束,期待keyword_end,但对于我的生活,我有结束语。我不知道它为什么没有运行。无论你说什么,它也只允许你运行淋浴==是的。如果你说不淋浴,它仍然输出“良好的呼叫”等任何帮助或直接将非常感激!

+4

小心你在这里提交什么样的代码。保持卫生。 – tadman

+4

我已经做了一些我们几乎从未在这里做过的事情,那就是编辑问题中的代码。我这样做是为了删除不适合SO的语言。我试图保留代码的含义是为了提出问题的目的,如果我没有这样做,我很抱歉。 –

+0

欢迎来到Stack Overflow。请阅读“[mcve]”。 –

回答

3

的问题是在这条线

if shower = "yes" or "sure" or "ok" 

请注意,这是shower = "yes"哪里是应该shower == "yes"。你所寻找的是这样的:

if shower == "yes" || shower == "sure" || shower == "ok" 

或类似这样的可读性

if ["yes", "sure", "ok"].include?(shower) 
+2

也值得考虑:'/ \ A \ s *(?: ye [ps] |是啊|确实|好(?:ay)?)\ s * \ z /'在这里可以让它更加松懈。 – tadman

1

重新缩进您的代码正确,你会看到有一个“结束”语句中缺少。另外,在您的第一个if中有一个错字,您想写==而不是=

关于条件,您的违规行为。在写exprA or exprB时,ruby评估exprA,exprB,如果其中一个为真,则返回true。所以,当你写shower == "yes" or "sure"时,Ruby评估shower == "yes"并评估"sure"

Ruby可以评估条件中的任何对象,唯一返回false的情况是声明为false或nil。所以,因为"sure"永远不会是假的也不是零,所以测试永远是真的。

您需要编写shower == "yes" or shower == "sure" or shower == "ok"

+3

严格地说,在一个'或'表达式中,Ruby不返回'true'或'false',它返回最后一个被评估的操作数。如果'exprA'真的会返回'exprA';如果'exprA'是假的,它将返回'exprB'。 –

+0

不知道,谢谢! –

相关问题