-
Notifications
You must be signed in to change notification settings - Fork 1
/
deaf_grandma5.rb
36 lines (29 loc) · 1.95 KB
/
deaf_grandma5.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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.
#Planning program version 1
#puts "Your comment to grandma"
#gets.chomp
#if capitals
#grandma puts "NO, NOT SINCE (randomly generated year between 1930-1950! Starts with 1938!"- srand)- this should be in a while loop
#else if lower case grandma puts HUH?! SPEAK UP, SONNY!
#else (?) "BYE" with an Enter to exit the program
#greeting = " "
def deaf_grandma(greeting)
puts greeting
#greeting = gets.chomp
if greeting == greeting.capitalize
puts "NO, NOT SINCE " + rand(101).to_s + " !"
while greeting != greeting.downcase
#puts "NO, NOT SINCE " + rand(1930-1950).to_s + " ."
greeting.gets.chomp
elsif greeting == greeting.downcase
puts "HUH?! SPEAK UP, SONNY!"
while greeting != greeting.capitalize
greeting.gets.chomp
else greeting == "BYE"
puts "BYE!"
end
end
greeting = " "