选择你自己的冒险 - 语法错误,意外的输入结束,期待keyword_end

时间:2016-05-27 18:27:26

标签: ruby

这是我到目前为止所做的:

    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但是对于我的生活,我有结束语。我不确定为什么它没有运行。它也只允许你运行淋浴==是的,不管你说什么。如果你拒绝淋浴,它仍会输出“好的电话”等任何帮助或直接将非常感谢!

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

正确地重新缩进代码,你会看到有一个"结束"声明遗失。此外,您的第一个if中有一个拼写错误,您要写==而不是=

关于条件,你的情况破了。当您编写exprA or exprB时,ruby会计算exprA,exprB,如果其中一个为true,则返回true。因此,当您编写shower == "yes" or "sure"时,Ruby会评估shower == "yes"并评估"sure"

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

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