I am going through the Chris Pine Ruby tutorial. In one of the exercises, I have to extend the program by making the user write "BYE" three times in a row to shut the program. I wanted to take it a step further and have Grandma respond on the third "BYE" before the program shuts down. Every time I type "BYE" i get two error messages, but the program still runs like it should aside from that. The error messages are:
DeafGrandma.rb:11: warning: already initialized constant BYE
DeafGrandma.rb:3: warning: previous definition of BYE was here
Here is my code:
# DeafGrandma
BYE = 0
while BYE < 3
puts "What do you want to say to Grandma?"
tell_grandma = gets.chomp
if tell_grandma == "BYE"
BYE += 1
end
if tell_grandma != tell_grandma.upcase
puts "HUH!? SPEAK UP, SONNY!"
else
puts "NO, NOT SINCE #{1929 + rand(22)}!"
end
end
while BYE = 3
puts "BYE BYE, SONNY!"
break
end
What must I change to get rid of these errors? Thank you in advance.
Constants start with capital letters and are just like variables, with the only exception that constants are meant to remain constant and not change throughout your program. In this case you're changing it by adding +=1 every time you mention 'BYE'.
Constants can not be reinitialized generally speaking, to fix your problem and get rid of the errors you need to change all references from BYE to bye
bye = 0
while bye < 3
puts "What do you want to say to Grandma?"
tell_grandma = gets.chomp
if tell_grandma == "BYE"
bye += 1
end
if tell_grandma != tell_grandma.upcase
puts "HUH!? SPEAK UP, SONNY!"
else
puts "NO, NOT SINCE #{1929 + rand(22)}!"
end
end
while bye = 3
puts "BYE BYE, SONNY!"
break
end
To expand on the answer given by Nabeel Amjad, a variable whose name begins with a capital letter is a 'constant' in Ruby. Of course, Ruby being Ruby, it's still possible to reassign these so-called 'constants' (because Ruby is flexible that way), but you get a warning, because that's not what constants are actually for, and in most cases when you reassign a constant it's a mistake.
You can fix it by simply renaming the variable BYE to bye (or bYE or the_number_of_times_the_user_said_bye)
This is what I did when doing Chris' Pine exercises:
puts "Start talking to Grandma. Sometimes she can't hear, so you might need to SPEAK UP! To leave just shout 'Bye'"
talk = gets.chomp
while talk != "BYE"
if talk == talk.downcase || talk == talk.capitalize
puts "HUH?! SPEAK UP, SONNY!"
talk = gets.chomp
elsif talk == talk.upcase
puts "NO, NOT SINCE" + " " + (1900 + rand(81)).to_s + "!"
talk = gets.chomp
elsif talk == "BYE"
break
end
end
Hope this helps! I had so much fun with this...Love Chris Pine's Learn to Program
Related
The task is taken from "Learn to Program" by Chrise Pine. The program is called 'Deaf Grandma'. Here's the task: "whatever you type, grandma (the program) should respond with this:
`HUH?! SPEAK UP, SONNY!`
unless you shout it (type in all capitals). In this case she responds with:
`NO, NOT SINCE 1938!`
Have Grandma shout a different year each time, maybe any year at random between 1930 and 1950. You have to shout BYE three times in a row. Make sure to test your program: if you shout
BYE three times but not in a row, you should still be talking to
Grandma."
Now, everything looks fine to me, except I didn't get where to put gets.chomp 3 times to exit a program. Eventually, I came up with this:
speak = gets.chomp
while speak != 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
if speak == speak.upcase
puts 'NO, NOT SINCE ' + (1930 + rand(20)).to_s + '!'
else repeat = gets.chomp
end
end
But in this case if I type BYE grandma still asks me:
`HUH?! SPEAK UP, SONNY!`
My question is: how can I properly make the program exit after I type BYE three times in a row?
Have a look at this, I've made some changes though. But should give you the expected output.
bye_count = 0
while true
speak = gets.chomp
if speak == 'BYE'
bye_count +=1
bye_count == 3 ? break : next
end
bye_count = 0 # Resets count
if speak == speak.upcase
puts 'NO, NOT SINCE ' + (1930 + rand(20)).to_s + '!'
else
puts 'HUH?! SPEAK UP, SONNY!'
end
end
You've not added anywhere in your code that using 'bye' 3 times will exit the program.
while bye < 3
Try looking at your code again and implementing the changes to exit after 3 byes.
Here's another way:
responses = ["", "", ""]
loop do
speak = gets.chomp
responses.shift
responses << speak
break if responses.all? { |r| r == "BYE" }
if speak == speak.upcase
puts 'NO, NOT SINCE ' + (1930 + rand(20)).to_s + '!'
else
puts 'HUH?! SPEAK UP, SONNY!'
end
end
Alternatively,
break if responses.uniq == ["BYE"]
puts "what can i do for your son?"
a=0
while a != 3
n= gets.chomp
if n.include? 'BYE'
puts "NO NOT SINCE #{rand(1897..1930)}".chomp
a = (a + 1)
end
if n != n.upcase
puts 'HUH?! SPEAK UP, SONNY!'.chomp
a=0
end
if n == n.upcase and n != 'BYE'
puts "NO NOT SINCE #{rand(1897..1930)}".chomp
a=0
end
end
I'm currently learning ruby from the Learn Ruby the hard way tutorial. And in that exercise, the author ask us to add things to a simple game. However, I was trying this to improve the bear_room method by doing something like this:
while true
print "> "
choice = gets.chomp.downcase!
if choice.include? ("taunt")
dead("The bear looks at you then slaps your face off.")
elsif choice.include? "taunt" && !bear_moved
puts "The bear has moved from the door. You can go through it now."
bear_moved = true
elsif choice.include? "taunt" && bear_moved
dead("The bear gets pissed off and chews your leg off.")
elsif choice.include? "open" && bear_moved
However, when I write this:
choice = gets.chomp.downcase!
It gives me this error when executing:
ex35.rb:44:in `bear_room': undefined method `include?' for nil:NilClass (NoMethodError)
from ex35.rb:99:in `start'
from ex35.rb:109:in `<main>'
But if do something like this:
choice = gets.chomp.downcase
or this:
choice = gets.chomp
choice.downcase!
It works. Why is that? I would appreciate any kind of help. Also, how works the while true bit? That really gets me confused.
Here is the rest of the program in case that you need it. I'm going to leave "separated" the mentioned method to make it easier to read.
# Creates the 'gold_room' method, so it can be called later.
def gold_room
puts "This room is full of gold. How much do you take?"
print "> "
choice = gets.chomp
# Converts the 'choice' variable to integer type.
choice.to_i
# Checks if 'choice' is equals to 0, OR greater or equal to 1.
if choice == '0' || choice >= '1'
# Saves the integer 'choice' variable in the 'how_much' variable.
how_much = choice.to_i
else
dead("Man, learn to type a number.")
end
# Checks if the 'how_much' variable is lesser than 50, and executes the code below if so.
if how_much < 50
puts "Nice, you're not greedy, you win!"
exit(0)
elsif how_much >= 50 && how_much < 100
puts "Mmm, ok that's enough. Get out!"
else
dead("You greedy bastard!")
end
end
################### bear_room method ###################
# Creates the 'bear_room' method.
def bear_room
puts "There is a bear here."
puts "The bear has a bunch of honey."
puts "The fat bear is in front of another door."
puts "How are you going to move the bear?"
puts "1. Taunt the bear."
puts "2. Steal the bear's honey. "
# Declares the 'bear_moved' variable as a boolean, initialize it to false.
bear_moved = false
while true
print "> "
choice = gets.chomp.downcase
if choice.include? ("taunt")
dead("The bear looks at you then slaps your face off.")
elsif choice.include? "taunt" && !bear_moved
puts "The bear has moved from the door. You can go through it now."
bear_moved = true
elsif choice.include? "taunt" && bear_moved
dead("The bear gets pissed off and chews your leg off.")
elsif choice.include? "open" && bear_moved
gold_room
else
puts "I got no idea what that means."
end
end
end
############### end of method ###############
# Defines the 'cthulhu_room' method.
def cthulhu_room
puts "Here you see the great evil Cthulhu."
puts "He, it, whatever stares at you and you go insane."
puts "Do you flee for your life or eat your head?"
print "> "
choice = gets.chomp
# Checks if the user's input contains the word 'flee'. If so, executes the code below.
if choice.include? "flee"
start
# Checks if the user's input contains the word 'head'. If so, executes the code below instead.
elsif choice.include? "head"
dead("Well that was tasty!")
else
# Otherwise, calls the 'cthulhu_room' method again.
cthulhu_room
end
end
# Defines the 'dead' method. It takes one argument (why). Example: dead("Well that was tasty!")
def dead(why)
puts why, "Nice."
# Succesfully finish the program.
exit(0)
end
# Defines the 'start' method, wich is where the game begins. Duh.
def start
puts "You are in a dark room."
puts "There is a door to your right and left."
puts "Which one do you take?"
print "> "
choice = gets.chomp
# Start the branching. It checks the user's input, and saves that on the 'choice' variable, which is used along the whole program in the other methods.
if choice == "left"
# Calls the 'bear_room' method.
bear_room
elsif choice == "right"
# Calls the 'cthulhu_room' method.
cthulhu_room
else
dead("You stumble around the room until you starve.")
end
end
# Start the game.
start
---------------------------------------------------------------------------
TL;DR: What's the difference between choice = gets.chomp.downcase! and
choice = gets.chomp
choice.downcase!
PS: The comments are part of the exercise. Please, if you have any type of correction (about the comments, how I made the question, code in general, etc) please tell me so I can improve. Thanks and sorry for the length!
-------------------------------------------------------------------------
That is because if the string did not change upon calling downcase! it returns nil. Thus when you try and call include? it says that nil does not have such a method.
Thus, it is safest to use the "non-destructive" version of downcase. The downcase! method mutates the string in place if it can.
Check the docs for further reading.
I am trying to answer the following question from Chris Pine "Learn to Program" book:
• Write a Deaf Grandma program. Whatever you say to grandma (whatever
you type in), she should respond with HUH?! SPEAK UP, SONNY!, unless
you shout it (type in all capitals). If you shout, she can hear you
(or at least she thinks so) and yells back, NO, NOT SINCE 1938! To
make your program really believable, have grandma shout a different
year each time; maybe any year at random between 1930 and 1950. (This
part is optional, and would be much easier if you read the section on
Ruby's random number generator at the end of the methods chapter.)
You can't stop talking to grandma until you shout BYE. Hint: Don't
forget about chomp! 'BYE'with an Enter is not the same as 'BYE'
without one! Hint 2: Try to think about what parts of your program
should happen over and over again. All of those should be in your
while loop.
• Extend your Deaf Grandma program: What if grandma doesn't want you
to leave? When you shout BYE, she could pretend not to hear you.
Change your previous program so that you have to shout BYE three times
in a row. Make sure to test your program: if you shout BYE three
times, but not in a row, you should still be talking to grandma.
My code is giving me the same "random_year" when I run it. Why is the code not providing me an actual random year (between 1930 and 1950)?
Code in ruby.
# Deaf Grandma
random_year = 1930 + rand(1950 - 1930)
puts 'WHAT DO YOU WANT KID?'
bye = 0
talk_to_grandma = nil
while bye < 3
talk_to_grandma = gets.chomp
if talk_to_grandma == "BYE"
puts 'NO, NOT SINCE ' + random_year.to_s
bye+=1
elsif talk_to_grandma == talk_to_grandma.upcase
puts 'NO, NOT SINCE ' + random_year.to_s
bye = 0
else
puts "HUH?! SPEAK UP, SONNY!"
bye = 0
end
end
You need to generate the random number inside your loop. Right now you generated it ONCE at the start of the program, and then it never changes. Basically:
while bye < 3
random_year = 1930 + rand(1950 - 1930) // move the random generation to here
puts 'NO, NOT SINCE ' + random_year.to_s
You have assigned a value to the random_year variable in the first code line. It is selected randomly.
On each call to
puts 'NO, NOT SINCE ' + random_year.to_s
the variable is accessed/read, but not modified.
You may want to encapsulate the random year generation in a method:
def random_year( start=1930, range = 20 )
start + rand( range )
end
then you can use it like this:
puts "NO, NOT SINCE #{random_year}"
or, for a slightly older grandma:
puts "NO, NOT SINCE #{random_year(1910, 50)}"
For that exercise I've used: rand(1930..1950)
I'm currently learning ruby, and I've gotten stuck on this problem:
Write a Deaf Grandma program. Whatever you say to grandma (whatever you type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you and yells back, NO, NOT SINCE 1938! To make your program really believable, have grandma shout a different year each time; maybe any year at random between 1930 and 1950. You can't stop talking to grandma until you shout BYE.
This is the code I tried:
puts "Say something to Grandma!"
something = gets.chomp
while something != "BYE"
if something == something.upcase
puts "NO, NOT SINCE 19" + (rand(30..50)).to_s + "!"
else
puts "HUH? SPEAK UP SONNY!"
end
end
Whenever I execute this, the if and else strings just go on an infinite loop. What am I doing wrong here?
You are only getting the input once, you need to read it at the beginning of each loop, like so:
something=""
while something != "BYE"
puts "Say something to Grandma!"
something = gets.chomp
if something == something.upcase
puts "NO, NOT SINCE 19" + (rand(30..50)).to_s + "!"
else
puts "HUH? SPEAK UP SONNY!"
end
end
Hope that makes sense.
Okay, I'm trying to write a ruby simulation of my grandmother. I can't quite get the loop to work the way I'd like though. I want granny to respond with
"OH, THAT REMINDS ME OF BACK IN (random year) ..."
when you answer her in all caps but I also want her to respond with
"WHAT'D YOU SAY????"
when you don't use all caps. I can get each one to work separately but I can't seem to make a continuous loop of granny with her crazy responses. Here's the code:
puts 'HELLO SONNY! WHAT\'S NEW IN THE WHO\'S IT WHAT\'S IT?'
response = gets.chomp
while response == response.upcase
puts 'OH, THAT REMINDS ME OF BACK IN ' + (rand(50) + 1905).to_s + '...'
response = gets.chomp
end
while response != response.upcase
puts 'WHAT\'D YOU SAY????'
response = gets.chomp
end
Any ideas?
The issue is that once you exit the first while loop, you're never returning back to it. Try something like this:
while true
response = gets.strip
if response == response.upcase
puts msg1
else
puts msg2
end
end
That'll run forever, until you decide to kill virtual-granny with Ctrl-C.
This program works, though Im a noob so it may not be the best way. Also my math is more creative then practical, the other guys is way better. :)
puts 'Talk to your grandma!'
while true
say = gets.chomp
if say == say.downcase
puts 'WHAT DID YOU SAY? SPEAK UP!'
else say == say.upcase
puts "NO HONEY, NOT SINCE 19" + (rand(90) + 10).to_s
end
break if say == 'bye'.upcase
end