Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlphonse Cuccurullo
2,513 PointsHow come it's saying undefined local variable withing my class? The variable is an array and its defined so now im lost.
Here is the class.... it is telling me in the pokemons method that on line 44 where it says chosen_6 there is a undefined variable. But i interpolated the variable so it should work.
class Pokemon
chosen_6 = ["Pikachu","Butterfree","pidgeotoo","bulbasaur","charmander","squirtle"]
chosen_6v2 =["Staryu","garadose","starmii","psyduck","politoad","togepi"]
chosen_6v3 =["geodude","onix","vulpix","zubat","crok","idk"]
def initialize(trainers,badges) @trainers = trainers @badges = badges
end
def trainers
case @trainers
when "Ash","ash"
puts "hi"
end
end
def bio
puts "Now loading bio of.......#{@trainers}"
case @trainers
when "ash","Ash"
puts "Ash ketchum is a pokemon trainer who one day dreams of being pokemon master."
when "Misty","misty"
puts "Misty is the curealn city gym leader and is a real bitch"
when "Brock","brock"
puts "Brocks a loser"
end
end
def badges
puts "#{@trainers} has this many badges #{@badges}."
if @badges < 8
puts "Oh im sorry #{trainers} does not have enough badges to enter league."
else
puts "Alright you have enough to enter."
end
end def pokemons case @trainers when "Ash","ash" puts "Ash's pokemon are #{chosen_6}" when "Misty","misty" puts "Misty calls #{chosen_6v2}" when "Brock","brock" put "Brock's pokemon are #{chosen_6v3}" end end end
ash = Pokemon.new("ash",8)
ash.trainers ash.bio ash.pokemons ash.badges puts "--------------------------" misty = Pokemon.new("misty\n",0) misty.bio misty.pokemons misty.badges
puts"---------------------------" brock = Pokemon.new("Brock",0) brock.bio brock.pokemons brock.badges
2 Answers
Marco van Vemden
12,553 PointsHi Alphonse, you will need to add an @ sign to the variable names to make them instance variables. Here is the working code:
class Pokemon
@chosen_6 = ["Pikachu","Butterfree","pidgeotoo","bulbasaur","charmander","squirtle"]
@chosen_6v2 =["Staryu","garadose","starmii","psyduck","politoad","togepi"]
@chosen_6v3 =["geodude","onix","vulpix","zubat","crok","idk"]
def initialize(trainers,badges)
@trainers = trainers
@badges = badges
end
def trainers
case @trainers
when "Ash","ash"
puts "hi"
end
end
def bio
puts "Now loading bio of.......#{@trainers}"
case @trainers
when "ash","Ash"
puts "Ash ketchum is a pokemon trainer who one day dreams of being pokemon master."
when "Misty","misty"
puts "Misty is the curealn city gym leader and is a real bitch"
when "Brock","brock"
puts "Brocks a loser"
end
end
def badges
puts "#{@trainers} has this many badges #{@badges}."
if @badges < 8
puts "Oh im sorry #{trainers} does not have enough badges to enter league."
else
puts "Alright you have enough to enter."
end
end
def pokemons
case @trainers
when "Ash","ash"
puts "Ash's pokemon are #{@chosen_6}"
when "Misty","misty"
puts "Misty calls #{@chosen_6v2}"
when "Brock","brock"
puts "Brock's pokemon are #{@chosen_6v3}"
end
end
end
ash = Pokemon.new("ash",8)
ash.trainers
ash.bio
ash.pokemons
ash.badges
puts "--------------------------"
misty = Pokemon.new("misty\n",0)
misty.bio
misty.pokemons
misty.badges
puts"---------------------------"
brock = Pokemon.new("Brock",0)
brock.bio
brock.pokemons
brock.badges
Marco van Vemden
12,553 PointsMove the "@chosen_" arrays to the initialize method, and it should work. Like this:
def initialize(trainers,badges)
@trainers = trainers
@badges = badges
@chosen_6 = ["Pikachu","Butterfree","pidgeotoo","bulbasaur","charmander","squirtle"]
@chosen_6v2 =["Staryu","garadose","starmii","psyduck","politoad","togepi"]
@chosen_6v3 =["geodude","onix","vulpix","zubat","crok","idk"]
end
Alphonse Cuccurullo
2,513 Pointsit worked mind i ask why it didnt say that im lacking arguements in m constructer?
Alphonse Cuccurullo
2,513 PointsAlphonse Cuccurullo
2,513 PointsHi thank you so much for the response it worked for the most part however in the string interpolation it didnt put the array. The chosens six arent being printed it just comes out blank. Do you know what that means?