# A simple program using branching to guess the answer

As a rookie to programming, i'm really excited about finishing my first program.

But I know I only learn by making mistakes and through constructive criticism.

I'm open to any feedback regarding my use of branching, and any way to "clean up" my code.

puts 'Hi! Pick a number between 0 and 10'
number = gets.chomp
puts 'Got it; your number is ' + number.to_s + ', right?'
check1 = gets.chomp

if
check1 == 'yes'
else
puts 'Oops. Either: A. I oopsie-daisied up or B. you\'re lying.'
end

number_doubled = gets.chomp
if
number_doubled.to_i == number.to_i * 2
puts 'cool. Now add 6. What\'s the new number?'
else
puts 'Oopsie-daisies'
end

number_plus_6 = gets.chomp
if
number_plus_6.to_i == number.to_i * 2 + 6
else
puts 'Oopsie-daisies'
end

number_halved = gets.chomp
if
number_halved.to_i == number_plus_6.to_i / 2
puts 'cool. Now, if you subtract the number you originally picked, ' + number + ', you\'re answer will be 3. Is it?'
else
puts 'Oopsie-daisies'
end

check2 = gets.chomp
if
check2 == 'yes'
puts 'See? I told you i was a smarty!!'
else
puts 'hmm, i think you\'re bad at math'
end

• I think it is a mistake of one sort or another to add expletives to the code. Seems a bit gratuitous. – David Aldridge Aug 25 '17 at 8:55
• Fair enough. But if the only criticism is my poor choice of gratuitous language, than I think you're talking more about your advanced skill in etiquette and less about the code. Which is a level of coding I hope to achieve, so thanks for looking! :) – J.R. Bob Dobbs Aug 28 '17 at 4:42

1. In Ruby, many people would prefer double-quoted strings, which allow you to: Got it; your number is #{number}, right?. Note that this also relies on an implicit #to_s on number. It also allows single quotes to be used without escaping.
2. Look out for variable naming. Naming things correctly is notoriously hard, even after years of experience, and number is a bit generic.
3. I would definitely put the if conditional on the same line as the if. If the expression is too long or a bit difficult to instinctively understand, assign its value to a variable with a meaningful name (e.g. correctly_added_six = number_plus_6.to_i == number.to_i * 2 + 6) so your condition is more easily understood (e.g. if correctly_added_six)