From 7eda8daa0b59a6f9c43c4b718cb7396bb2e9ac9e Mon Sep 17 00:00:00 2001 From: thenora <55004911+thenora@users.noreply.github.com> Date: Wed, 5 Feb 2020 10:29:39 -0800 Subject: [PATCH 1/2] Create calculator-nr.rb I've made tweaks this morning that I need to clean up, but I'm submitting to practice submitting. --- calculator-nr.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 calculator-nr.rb diff --git a/calculator-nr.rb b/calculator-nr.rb new file mode 100644 index 0000000..bcfdcc7 --- /dev/null +++ b/calculator-nr.rb @@ -0,0 +1,69 @@ +puts "Welcome to Nora's Basic Calculator." + +puts "Please choose one of these commands:" + +# the commands the calculator will recognize +maths = ['add', '+', 'subtract', '-', 'multiply', '*', 'divide', '/'] + +maths.length.times do |i| + puts "#{maths[i]}" +end + +puts "What's your choice? " +command = gets.chomp.downcase + +until maths.include?(command) + puts "Please tell me to add (+), subtract (-), multiply (*), or divide (/)!" + command = gets.chomp.downcase +end + +puts "Enter the first number: " +user_num1 = gets.chomp +intCheck = Integer(user_num1) rescue false +until intCheck + puts "First number must be an integer: " + user_num1 = gets.chomp + intCheck = Integer(user_num1) rescue false +end +user_num1 = user_num1.to_f + +puts "Enter a second number: " +user_num2 = gets.chomp +intCheck = Integer(user_num2) rescue false +until intCheck + puts "Second number must be an integer: " + user_num2 = gets.chomp + intCheck = Integer(user_num2) rescue false + # I want to figure out how to make sure the input value is not 0 if user chose division (so they cannot divid by 0 down below) +end +user_num2 = user_num2.to_f + +puts "********" + +def calc_subtract(num1, num2) + puts "#{num1} - #{num2} = #{num1 - num2}" +end + +def calc_add(num1, num2) + puts "#{num1} + #{num2} = #{num1 + num2}" +end + + +case command + when "add", "+" + calc_add(user_num1, user_num2) + when "subtract", "-" + calc_subtract(user_num1, user_num2) + when "multiply", "*" + puts "#{user_num1} * #{user_num2} = #{(user_num1 * user_num2)}" + when "divide", "/" + # Only need to check if the bottom number is 0 + if user_num2 != 0 + puts "#{user_num1} / #{user_num2} = #{user_num1 / user_num2}" + else + puts "Sorry, you can't divide by 0." + end + end + + +puts "That's some basic math for you!" From 938456fcf65b312baf6d02ee05a2b2fb877f86fd Mon Sep 17 00:00:00 2001 From: thenora <55004911+thenora@users.noreply.github.com> Date: Wed, 5 Feb 2020 13:50:04 -0800 Subject: [PATCH 2/2] It's working now Fixed errors I'd introduced this morning --- calculator-nr.rb | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/calculator-nr.rb b/calculator-nr.rb index bcfdcc7..592ea1d 100644 --- a/calculator-nr.rb +++ b/calculator-nr.rb @@ -5,41 +5,44 @@ # the commands the calculator will recognize maths = ['add', '+', 'subtract', '-', 'multiply', '*', 'divide', '/'] +# NOTE I struggled with getting validation to work +# validate from class +# def validate_num[num] +# is_valid = num.to_i.to_s == input || num.to_f.to_s == num +# return is_valid +#end + +# print the list of commands maths.length.times do |i| puts "#{maths[i]}" end +# user picks type of operation puts "What's your choice? " command = gets.chomp.downcase +# verify the user's operation is in our command list until maths.include?(command) puts "Please tell me to add (+), subtract (-), multiply (*), or divide (/)!" command = gets.chomp.downcase end puts "Enter the first number: " -user_num1 = gets.chomp -intCheck = Integer(user_num1) rescue false -until intCheck - puts "First number must be an integer: " - user_num1 = gets.chomp - intCheck = Integer(user_num1) rescue false -end -user_num1 = user_num1.to_f +user_num1 = gets.chomp.to_i -puts "Enter a second number: " -user_num2 = gets.chomp -intCheck = Integer(user_num2) rescue false -until intCheck - puts "Second number must be an integer: " - user_num2 = gets.chomp - intCheck = Integer(user_num2) rescue false - # I want to figure out how to make sure the input value is not 0 if user chose division (so they cannot divid by 0 down below) -end -user_num2 = user_num2.to_f +# NOTE - second part of my validation code I couldn't get to work +# if validate_num[user_num1] == false +# puts "Oops. Please enter a number: " +# user_num1 = gets.chomp +# else + puts "Enter a second number: " + user_num2 = gets.chomp.to_i +# end -puts "********" +puts "**********" + +# NOTE since my validation wasn't working I made some not very useful methods just to demonstrate making a method def calc_subtract(num1, num2) puts "#{num1} - #{num2} = #{num1 - num2}" end @@ -48,7 +51,7 @@ def calc_add(num1, num2) puts "#{num1} + #{num2} = #{num1 + num2}" end - +# choose the type of operation and output based on the command case command when "add", "+" calc_add(user_num1, user_num2) @@ -57,13 +60,11 @@ def calc_add(num1, num2) when "multiply", "*" puts "#{user_num1} * #{user_num2} = #{(user_num1 * user_num2)}" when "divide", "/" - # Only need to check if the bottom number is 0 - if user_num2 != 0 + if user_num1 != 0 && user_num2 != 0 puts "#{user_num1} / #{user_num2} = #{user_num1 / user_num2}" else puts "Sorry, you can't divide by 0." end end - puts "That's some basic math for you!"