0
当我试图做一个循环我的代码,它突然问我这个:未定义的局部变量或方法'停止”主:对象(NameError)
未定义的局部变量或方法`停'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
欢迎堆栈溢出。请阅读你的问题,并想象你已经被同事问过同样的问题。你想看到什么来帮助回答这个问题?将此添加到问题中。正如你的问题是非常模糊,需要详细信息,如果我们要给你任何详细的帮助。 –
对不起,代码变得有点怪怪 –
停止应该是“停止”。如果你没有用引号括起来,系统会认为它是一个变量,这就是为什么你会得到这个错误。 – artsylar