undefined局部变量或方法`stop'for main:Object(NameError)

时间:2015-12-15 00:39:06

标签: ruby loops

当我试图为我的代码制作循环时,突然它问我:

  

未定义的局部变量或方法`stop'for main:Object(NameError)。

如何解决此错误?

loop do
  puts "Hello. I am a calculator. Please tell me your name."
  name = gets
  puts "Hi #{name}. Enter 1 to add, 2 to multiply, 3 to subtract, and 4 to divide."
  z = gets.chomp.to_i

  def addition
    puts "Please enter your first number"
    a = gets.chomp.to_i
    puts "Please enter your second number"
    b = gets.chomp.to_i
    puts a+b
  end

  def multiply
    puts "Please enter your first number"
    c = gets.chomp.to_i
    puts "Please enter your second number"
    d = gets.chomp.to_i
    puts c*d
  end

  def subtract
    puts "Please enter your first number"
    e = gets.chomp.to_i
    puts "Please enter your second number"
    f = gets.chomp.to_i
    puts e-f
  end

  def divide
    puts "Please enter your first number"
    h = gets.chomp.to_i
    puts "Please enter your second number"
    g = gets.chomp.to_i
    puts h/g
  end

  if z == 1
    puts addition
  elsif z == 2
    puts multiply
  elsif z == 3
    puts subtract
  elsif z == 4
    puts divide
  end

  puts "Would you like to calculate something else or stop?"
  choice = gets.chomp.to_s

  if choice == stop
    break
  end
end

1 个答案:

答案 0 :(得分:2)

问题是你有这个:

if choice == stop

我认为你的意思是:

if choice == 'stop'