Help with my code please!


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-JdNDe/0/8?curriculum_id=5059f8619189a5000201fbcb


The program runs fine; however, I am facing some limitations that I need help overcoming.
The first limitation is the option of not entering a string at the prompt. Code Academy suggests adding a second if statement to address this; however, if at the second prompt the user chooses not to enter a string (again), the program simply ends, rather than printing my "else" statement. I'd like the program to handle the second string the same way it would the first.

Second, I tried to account for all the variations of a daffy duck lisp, and not just words with "c" that sound like "s" [user_input.gsub!("ce", "th")]. So I went all out, as you can see below, trying to account for words with "st", "ss", and "sh". However, it only substitutes "th" for the first letter, yet works fine with "ce."

For example: If you enter "Not a chance" the output will read: "Not a chanth" which is perfect.
However, if you enter: "Sham," the output will read: "Thham" - notice the extra "h" ?

Any tips? Thank you!


print "Tell it like it is:"
user_input = gets.chomp

if user_input.include?("S") || user_input.include?("s") || 
user_input.include?("ce")
    
    user_input.gsub!(/s/, "th")
    user_input.gsub!(/S/, "Th")
    user_input.gsub!("ce", "th")
    user_input.gsub!("sh", "th")
    user_input.gsub!("Sh", "Th")
    user_input.gsub!("ss", "th")
    user_input.gsub!("st", "th")
    user_input.gsub!("St", "Th")
    print "#{user_input}"
    
elsif user_input == "" 
    print "This is your FINAL chance, to REALLY tell it like it is... "
    user_input = gets.chomp

    user_input.include?("S") || user_input.include?("s") || 
    user_input.include?("ce")    
    user_input.gsub!(/s/, "th")
    user_input.gsub!(/S/, "Th")
    user_input.gsub!("ce", "th")
    user_input.gsub!("sh", "th")
    user_input.gsub!("Sh", "Th")
    user_input.gsub!("ss", "th")
    user_input.gsub!("st", "th")
    user_input.gsub!("St", "Th")
    print "#{user_input}"

else
    print "Daffy Duck would never say that..."

end


#2

Figured it out! I was already replacing single "s" with "th". So if I entered the word sham, "s" would become "th" before the gsub command could even pick-up the "sh" or "st" sound! So it would read "thham".

Now with the code in this order, it works beautifully!

user_input.include?("S") || user_input.include?("s") || 
user_input.include?("ce")    

user_input.gsub!("sh", "th")
user_input.gsub!("Sh", "Th")
user_input.gsub!("ss", "th")
user_input.gsub!("st", "th")
user_input.gsub!("St", "Th")
user_input.gsub!(/s/, "th")
user_input.gsub!(/S/, "Th")
user_input.gsub!("ce", "th")
print "#{user_input}"

Sidenote: St shouldn't be changed... Daffy duck would totally say "Thith ith a thtand off" not a "thand off"... lol oh well


#3

Still need help with resolving an empty response at the SECOND prompt... Any tips/direction??


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.