Created
June 8, 2015 18:37
-
-
Save falonofthetower/8955e0302808e8180d25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def say(msg) | |
puts "=> #{msg}" | |
end | |
loop do | |
say "What's the first number?" | |
num1 = gets.chomp | |
say "What's the second number?" | |
num2 = gets.chomp | |
operator = '' | |
loop do | |
puts "1) add 2) subtract 3) multiply 4) divide" | |
operator = gets.chomp | |
break if %w(1 2 3 4).include?(operator) | |
puts "Please choose one of the following options:" | |
end | |
calc_num1 = num1.to_f | |
calc_num2 = num2.to_f | |
case | |
when operator == '1' | |
result = calc_num1 + calc_num2 | |
when operator == '2' | |
result = calc_num1 - calc_num2 | |
when operator == '3' | |
result = calc_num1 * calc_num2 | |
when operator == '4' | |
while calc_num2 == 0.0 | |
puts "You can't divide by 0. Your fist number is #{num1}. Please choose a new second number to divide by." | |
num2 = gets.chomp | |
calc_num2 = num2.to_f | |
end | |
result = calc_num1 / calc_num2 | |
end | |
#Output result. If resulting float is of the form 'n.0', prints result without the trailing 0. | |
if result/result.to_i == 1.0 | |
puts "The result is #{result.to_i}." | |
else | |
puts "The result is #{result}." | |
end | |
say "Do you want to do more math? (y or n)" | |
break if gets.chomp.downcase != 'y' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment