首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chris Pine学会编程Ruby CH9:心理测试程序

Chris Pine学会编程Ruby CH9:心理测试程序
EN

Stack Overflow用户
提问于 2012-01-06 03:40:56
回答 3查看 327关注 0票数 0

所以我跟着做了心理测试墨西哥卷饼,玉米饼,wets_bed程序在克里斯·派恩...是的,我改成了"Escape: the Pina Colada song“的歌词,但除此之外,保持不变。

然而,它在第一个“请求”时就卡住了。。。帮助?

我不想大刀阔斧地改变这一点,只是想找出程序挂起的地方。

代码语言:javascript
复制
# Nice little questionnaire 

def ask question
  good_answer = false
  while (not good_answer)
    puts question
    reply = gets.chomp.downcase

    if (reply == 'yes' or reply == 'no' )
      good_answer == true
      if reply == 'yes'
        answer = true
      else
        answer = false
      end
    else 
      puts 'Please answer "yes" or "no".'
    end
  end

  answer # This is what we return (true or false)
end

puts 'Hello, and thank you for smoking.'

puts

ask 'Do you like pina-coladas?'
ask 'Do you like getting caught in the rain?'
risky_business = ask 'Do you know what your sig other likes?'
ask 'Are you into yoga?'
ask 'Do you have half a brain?'
puts 'Just a few more questions.'
ask 'Do like the feel of the ocean?'
ask 'Are you into champagne?'

puts
puts 'DEBRIEFING'
puts 'Thank\'s for all the fish'
puts
puts risky_business

就这样。我想知道这是否可能是Ruby版本的问题?

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-06 03:46:56

这行有问题吗?good_answer == true

难道不应该是good_answer = true吗?

票数 6
EN

Stack Overflow用户

发布于 2012-01-06 05:38:08

如果你打开了警告,Ruby本身就会告诉你good_answer == true行没有意义。

您可以在代码中使用$VERBOSE = true打开警告,或者让unix变量RUBYOPT包含-w

票数 1
EN

Stack Overflow用户

发布于 2012-01-06 04:25:11

顺便说一句,我会像这样重写ask函数,使其更符合Ruby的方式:

代码语言:javascript
复制
def ask(question)
  puts question

  while reply = gets.chomp.downcase
    if %w( yes no ).include?(reply)
      answer = if reply == 'yes' then true else false end
      break
    else
      puts 'Please answer "yes" or "no".'
    end
  end

  answer
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8748672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档