Learning ruby on Codacademy


#1

Hi everybody.

I am doing the lesson called “Formating with String method”, I think I did it right because I got the good result by apparently no. I 'll share my code with you, please let me know if you see somethong wrong.

Thank you so much!

print “what is your first name?”
first_name = gets.chomp
first_name = “olfa”
puts “olfa”.capitalize

print “What is your last name?”
last_name = gets.chomp
last_name = “maslah”
puts “maslah”.capitalize

print “Which city are you from?”
city = gets.chomp
city = “paris”
puts “paris”.capitalize

print “Which State are you from?”
state = gets.chomp
state = “france”
puts “france”.upcase


#2

so you are getting user input:

first_name = gets.chomp

and storing this in a variable named first_name

then you overwrite the user input:

first_name = "olfa"

why? Getting user input and then overwriting the user input doesn’t sound very practical to me

finally you print a string:

puts "olfa".capitalize

so you don’t even use the first_name variable in the end, why?

this also applies to all the others


#3

you’re right, it wasn’t really necessary. It tried without and with. I will remove it.


#4

without what and with what? Its highly recommended to use specific names here, given i have now no idea what you tried

same here, no idea what it is referencing.


#5

sorry, I tried with adding name = “olfa” and without adding it, don’t change anything


#6

first_name you mean?

then when you print (puts) the user input, you should use the variable (first_name)

what do you have now and why is it still failing? You do understand that you need to provide input to gets.chomp (its a prompting function, like a input field but then for command line) when the program is running?


#7

Oh yes I mean first_name.

I have now:

print "what is your first name?"
first_name = gets.chomp
puts "olfa".capitalize

print "What is your last name?"
last_name = gets.chomp
puts "maslah".capitalize

print "Which city are you from?"
city = gets.chomp
puts "paris".capitalize

print "Which State are you from?"
state = gets.chomp
puts "france".upcase

and I obtain this

what is your first name?olfa
Olfa
What is your last name?Maslah
Which city are you from?Paris
Which State are you from?FRANCE


#8

This is the instrcutions, maybe it will be more clear:
After each variable assignment: first_name , last_name , and city add the .capitalize! method

For state use the .upcase! method


#9

so regardless of what the user input as first_name, the output will always OLFA

the idea of this program is to print a uppercase version of the user first name/first name entered by user of your program


#10

I don’t understang. why the correction say it is wrong, do you see any other mistake?

PS: sorry for my english, I am french so I do my best.


#11

yes, i do, and i explain the mistake here:

when i run your program:

your program outputs: Olfa

The program should output the the input entered by the user. So in this specific case: Lisa

which is not happening, which is why the lesson is rejecting your code


#12

Ok thank you so I should do
puts “first_name”.capitalize


#13

if you do puts "first_name".capitalize you nearly have the same problem as i mentioned earlier:

except this time the output would be First_name instead of Olfa. While it should be Lisa (following the previous example)


#14

Yes I get it. I try another way.


#15

Understanding the difference between strings ("first_name") and variables (first_name) is quite crucial if you want to get your program to work


#16

Ok I understand the differences if I puts “Olfa”.capitalize it will provide always Olfa…but if I define a input variable it will provide the user_name so it could be different answers. I will keep trying with this. thank you!


#17

Do you this version is better?

print "what is your first name?"
first_name = gets.chomp.capitalize
puts "#{first_name}"

print "What is your last name?"
last_name = gets.chomp.capitalize
puts "#{last_name}"

print "Which city are you from?"
city = gets.chomp.capitalize
puts "#{city}"

print "Which State are you from?"
state = gets.chomp.upcase
puts "#{state}"

#18

I find the right way, it’s this one.

print “what is your first name?”
first_name = gets.chomp
puts “#{first_name.capitalize!}”

print “What is your last name?”
last_name = gets.chomp
puts “#{last_name.capitalize!}”

print “Which city are you from?”
city = gets.chomp
puts “#{city.capitalize!}”

print “Which State are you from?”
state = gets.chomp.upcase
puts “#{state.upcase!}”

Thank you so much for your help and your time.


#19

I don’t see the value of inserting the variables into a string here, we could just use the variable directly:

puts first_name.capitalize

#20

intresting very intresting i myself am working on java script i think lol i cant even rember what imworkign on just wanted to ask an you make your own projects n here when you’ve learned enough.