Created
August 12, 2016 05:28
-
-
Save livmaria7891/908234858c5937371386451ab3f8c329 to your computer and use it in GitHub Desktop.
This file contains 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
puts "Hey! I'm a calculator!" | |
# Collects and checks for appropriate user input | |
print "I can add, subtract, divide, multiply, do exponents, and find a remainder. What would you like to do? " | |
operation = gets.chomp | |
possible_operations = ["+","add","addition","-","subtract","subtration","*","multiply","multiplication","/","divide","division","**","exponent","exponents","%","remainder","modulo"] | |
until possible_operations.include? operation | |
print "\n\nWomp Womp. Like I said, I can add, subtract, divide, multiply, do exponents, and find a remainder. What would you like to do? " | |
operation = gets.chomp | |
end | |
print "Cool, cool. Which two numbers?\n" | |
num1 = gets.chomp | |
num2 = gets.chomp | |
if num1.include?(".") == true || num2.include?(".") == true | |
num1 = num1.to_f | |
num2 = num2.to_f | |
else | |
num1 = num1.to_i | |
num2 = num2.to_i | |
end | |
puts "\n\nMy work here is done:" | |
case operation | |
when "+","add","addition" | |
ans = num1 + num2 | |
puts "#{num1} + #{num2} = #{ans}" | |
when "-","subtract","subtraction" | |
ans = num1 - num2 | |
puts "#{num1} - #{num2} = #{ans}" | |
when "*","multiply","multiplication" | |
puts "#{num1} * #{num2} = #{num1 * num2}" | |
when "/","divide","division" | |
puts "#{num1} / #{num2} = #{num1 / num2}" | |
when "**" , "exponents" | |
puts "#{num1} ** #{num2} = #{num1 ** num2}" | |
when "%", "remainder", "modulo" | |
puts "#{num1} % #{num2} = #{num1 % num2}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple and clearly done. How could you prevent incorrect input at the command line?