Ruby 用户输入+;随机字和数字打印

Ruby 用户输入+;随机字和数字打印,ruby,methods,Ruby,Methods,所以我有代码: puts 'What is your name?(Enter in field below)' input = gets.chomp puts 'end' occupationslist = ['Engineer', 'Clerk', 'Doctor', 'Demolition Expert', 'Athlete', 'None',] oclistlength = occupationslist.length rand1 = rand(oclistlength) occupati

所以我有代码:

puts 'What is your name?(Enter in field below)'
input = gets.chomp
puts 'end'
occupationslist = ['Engineer', 'Clerk', 'Doctor', 'Demolition Expert', 'Athlete', 'None',]
oclistlength = occupationslist.length
rand1 = rand(oclistlength)
occupation = ocupationslist[rand1]
def occupations
  puts input
  puts 'Occupation: ' + occupation
  puts 'Rating: ' + rand(1-12).to_s  
end
occupations
它应该显示你的名字(你输入的),一个随机职业,和一个随机评级,但我不知道它有什么问题。 这是令人满意的输出:

prints "What is your name?".
(gets user input)
prints out the input.
prints out a random 'occupation'(from the list in the array above).
prints out the 'Rating: ' - a random number from 0 to 12.

输入
职业
职业
功能范围之外定义。使用
$input
$occulation
将其声明为全局变量,在函数中声明它,或将变量作为参数传递给函数(如Lee所建议):


此外,还有一个输入错误:在
occulation=occulationslist[rand1]
中,而不是在oc*c*upationslist[rand1]中,您编写了ocupationslist[rand1](没有“c”)。

Us——症状是什么?会发生什么?会发生什么?这些问题应该是具体的。您既不提供细节也不提供上下文,希望人们(a)猜测您希望代码做什么,(b)猜测(或运行)代码以确定它实际做了什么,(c)猜测两者之间可能的增量,以及(d)以对您有意义的方式解释它。这似乎是寻求帮助的合理方式吗?我建议不这样做,正如投票所证明的——YMMV。我想删除这个问题,但我不能。你可以标记它以引起主持人的注意,并要求他们删除它(不保证,尽管他们可能会很差),或者编辑问题,使其有意义。简单地丢弃一些代码并说“它不起作用,我不知道为什么”是愚蠢的。添加“它应该这样做”并没有多大好处,因为我们不知道它能做什么。失败的确切症状是什么?这与预期产出相比如何?你试过怎么解决它?你需要帮助我们帮助你,特别是因为你要求免费帮助——不要让人们跳过不必要的障碍。我们喜欢帮忙。我们喜欢编程。我们想要教育。我们不希望有人在我们身上撒一堆狗屎,让我们猜所有的东西。或者,更好的是,把输入和职业作为参数传递。谢谢,这个程序是我为了好玩而写的,应该只是以一种娱乐的方式运行。发布问题5分钟后,我发现了大写错误,但我对全局变量一无所知谢谢。使用globals传递参数是个坏主意。Ruby社区不鼓励全球用户做任何事情。为什么不直接使用参数呢?
puts 'What is your name?(Enter in field below)'
$input = gets.chomp
puts 'end'
occupationslist = ['Engineer', 'Clerk', 'Doctor', 'Demolition Expert', 'Athlete', 'None',]
oclistlength = occupationslist.length
rand1 = rand(oclistlength)
$occupation = occupationslist[rand1]
def occupations
  puts $input
  puts 'Occupation: ' + $occupation
  puts 'Rating: ' + rand(1-12).to_s  
end
occupations