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 trialCaleb Kleveter
Treehouse Moderator 37,862 PointsIssue with ruby.
I was writing a fun little ruby program and I got this warning when I ran it in terminal: "fun.rb:15: warning: string literal in condition", it ran but only the last elsif will work, here is my code:
print "You are attempting to use the the fun.rb program\nPlease enter your password:\n"
password = gets
if password == "Destroy"
puts "Run for your life, This computer while self-destruct\nafter this 10 second count down is finished"
elsif password == "I don't know the password"
puts "Type on!"
type = gets
elsif password != "Destroy" || "I don't know the password"
puts "That is an incorrect password,\nthis programm will know terminate"
end
Line 15 is "end".
4 Answers
Ayoub AIT IKEN
12,314 PointsHey ! in the last check, its not necessary that you recheck, what the password is, since its gonna print the last message no matte what the response is, you jsut put an else, like telling the program, whatever the answer is if it is different to what is above, then print this.
print "You are attempting to use the the fun.rb program\nPlease enter your password:\n"
password = gets.chomp
if password == "Destroy"
puts "Run for your life, This computer while self-destruct\nafter this 10 second count down is finished"
elsif password == "I don't know the password"
puts "Type on!"
type = gets.chomp
else
puts "That is an incorrect password,\nthis programm will know terminate"
end
PS : I dt see what the role of your type variable in the code !
Caleb Kleveter
Treehouse Moderator 37,862 PointsHi Ayoub, Thanks for the help, at this point here is what my code looks like:
puts "You are attempting to use the the fun.rb program\nPlease enter your password:"
password = gets.chomp
if password == "Destroy"
puts "Run for your life, This computer while self-destruct\nafter this 10 second count down is finished"
elsif password == "I don't know the password"
print "Type on!"
else
puts "That is an incorrect password,\nthis programm will know terminate"
end
What I want now is to allow typing of any sort if someone enters "I don't know the password", that is what I was attempting to do with the type variable. Do I need to create a method?
Ayoub AIT IKEN
12,314 Pointsif the user types in " I dont know the password ", what do you want to do after that exactly !
Caleb Kleveter
Treehouse Moderator 37,862 PointsI want the user to be able to type in the program, not that it will do anything I just want that ability.
Caleb Kleveter
Treehouse Moderator 37,862 PointsI got it, though I did change what the program says, here is the code:
puts "You are attempting to use the the fun.rb program, please enter your password: "
password = gets.chomp
if password == "destruct"
puts "Run for your life, this computer while self-destruct after this 10 second count down is finished"
elsif password == "c@!&%'$"
print "You may type now: "
conntent = gets.chomp
puts "Your content will not be saved."
else
puts "That is an incorrect password, this program will know terminate"
end
The password "c@!&%'$" is supposed to spell "caleb's" in symbols.
Ayoub AIT IKEN
12,314 PointsWell Done Caleb Kleveter , keep it up !
Caleb Kleveter
Treehouse Moderator 37,862 PointsCaleb Kleveter
Treehouse Moderator 37,862 PointsThanks for all your help Ayoub!