未定义的局部变量错误

时间:2016-10-29 19:47:16

标签: ruby

运行以下代码时,继续收到未定义的本地变量错误。问题似乎源于与我的gets.chomp有关的事情。 我已经包含了整个程序,因此有一些上下文。问题似乎来自第16行,但即使我删除了第一个,我也会从下一个gets.chomp获得相同的错误。

def test()
  score = 0
  def scorekeeper(input, answer)
  if input == answer
      puts "Correct!"
      score += 10
      puts score
  else
      puts "incorrect :("
  end
  end

  puts "Welcome to Virtual Math Test V1.0.0"
  sleep 0.7
  puts "What is Your Name?"
  gets.chomp = username

  puts "Question 1) \n 24 * 24"
  gets.chomp = ans1
  scorekeeper(ans1, 24 * 24)
  system "clear"

  puts "Question 2) \n 28 / 4"
  gets.chomp = ans2
  scorekeeper(ans2, 28 / 4)
  system "clear"

  puts "Question 3) \n 754 + 222"
  gets.chomp = ans3
  scorekeeper(ans3, 976)
  system "clear"

  puts "Well Done!"
  puts "#{username} scored #{score} points out of 30!"
  sleep 0.4
  puts "do you want to take the test again? y/n"
  gets.chomp.downcase = repeatyn

  if repeatyn == y || yes
      test()
  else
      abort("Ok, See you later #{username}!")
  end
end

test()

谢谢!

1 个答案:

答案 0 :(得分:1)

你的作业是错误的,下面的

gets.chomp = username
gets.chomp = ans1
gets.chomp = ans2
gets.chomp = ans3
gets.chomp.downcase = repeatyn

应改写为:

username = gets.chomp
ans1 = gets.chomp
ans2 = gets.chomp
ans3 = gets.chomp
repeatyn = gets.chomp.downcase

分别