Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maria McGrew - Carets - calculator #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions c8_calc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
puts "Let's crunch some numbers!"
puts "---------------------------------"

def operation
operator_hash = {"add" => '+', "+" => "+", "subtract" => "-" , "-" => "-", "multiply" => "*", "*" => "*", "divide" => "/", "/" => "/"}
print "Enter an operator: "
math_verb = gets.chomp.downcase
until operator_hash.has_key?(math_verb)
print "Enter an operator: "
math_verb = gets.chomp.downcase
end
return operator_hash[math_verb]
end

def num_input
print "Enter a number: "
number = Float(gets.chomp) rescue nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do be careful when doing this. We haven't talked about rescue yet, but later on a solution like this for other things could be frowned upon. More info is here

until number.is_a?(Float) do
print "This is not a number. Please enter a number: "
number = Float(gets.chomp) rescue nil
end
return number
end

def calculation(num1, operation, num2)
case operation
when "+"
return num1 + num2
when "-"
return num1 - num2
when "*"
return num1 * num2
when "/"
if num2 == 0
puts "You can't divide by 0"
else
return num1 / num2
end
end
end

def final_calculation
num1 = num_input
mode = operation
num2 = num_input
total = calculation(num1, mode, num2)
puts "#{num1} #{mode} #{num2} = #{total}"
end

final_calculation