变量未定义

瑞安克罗克

我只是在玩弄试图理解它的东西

:/Ruby Tutorials/dragonhealth.rb:9:in `<main>': undefined local variable or met
od `toughness' for main:Object (NameError)

而且我一生都无法弄清楚为什么未定义变量。它很好地接受了gets.chomp.to_i的输入,这行似乎很简单,正在中断它

health + armor = toughness


def dragon_toughness(health, armor)
puts "The dragon's health is #{health}!"
puts "The dragon's armor is #{armor}!"
end
puts "What is the dragon's health?"
health = gets.chomp.to_i
puts "What is the dragon's armor?"
armor = gets.chomp.to_i

health + armor = toughness

dragon_toughness(health, armor)

if toughness > 40
puts "Wow, tough dragon!"
elsif
    toughness <= 40
puts "That dragon is kinda weak son!"

end
奥雅纳·拉希特(Arup Rakshit)

换线

  health + armor = toughness

作为

  toughness = health + armor

完整代码(粘贴在我的文件中so.rb):

def dragon_toughness(health, armor)
  puts "The dragon's health is #{health}!"
  puts "The dragon's armor is #{armor}!"
end

puts "What is the dragon's health?"
health = gets.chomp.to_i
puts "What is the dragon's armor?"
armor = gets.chomp.to_i

toughness = health + armor

dragon_toughness(health, armor)

if toughness > 40
  puts "Wow, tough dragon!"
elsif
  toughness <= 40
  puts "That dragon is kinda weak son!"  
end

让我们运行它:

(arup~>Ruby)$ ruby so.rb
What is the dragon's health?
12
What is the dragon's armor?
13
The dragon's health is 12!
The dragon's armor is 13!
That dragon is kinda weak son!
(arup~>Ruby)$ 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章