Skip to content

Branches - Michaela #35

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

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
80 changes: 80 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Define data structures
operators = {
"add": "+",
"+": "+",
"subtract": "-",
"-": "-",
"multiply": "*",
"*": "*",
"divide": "/",
"/": "/",
"modulo": "%",
"%": "%",
"exponent": "**",
"^": "**"
}

# Method to print all valid operators
def print_operators
puts "1. Add (+)"
puts "2. Subtract (-)"
puts "3. Multiply (*)"
puts "4. Divide (/)"
puts "5. Modulo (%)"
puts "6. Exponent (^)"
print "\nPlease choose an operator (name or symbol): "
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I love this method!!


# Method to keep requesting input until a valid operator is entered
def error_message_operators(input, valid_input)
until valid_input.keys.include? input.to_sym
puts "Oops! Your input was invalid. Your options are:"
print_operators
input = gets.chomp
end
return input
end

# Method to keep requesting input until a number is entered
def error_message_numbers(input)
while input == nil
puts "Oops! Your input was invalid. Please enter a number: "
input = Float(gets) rescue nil
end
return input
end

# Greet the user
puts "Welcome to your handy dandy calculator! Which operator would you like to use?"
print_operators

# Check user-input operator for validity and store valid operator
chosen_operator = gets.chomp.downcase
chosen_operator = error_message_operators(chosen_operator, operators)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The error_message_operators method is so so so clever. I like it a lot! Keep doing things like this! It may be interesting to rename this method to something like get_operator, and refactor the code so that you only pass in operators (instead of chosen_operator and operators) and also get the initial user operator in the method, too. For example, consider:

def get_operator(valid_input)
  input = gets.chomp.downcase
  until valid_input.keys.include? input.to_sym
    puts "Oops! Your input was invalid. Your options are:"
    print_operators
    input = gets.chomp
  end
  return input
end


print "\nPlease enter your first number: "

# Check user-input numbers for validity and store valid numbers
chosen_number_1 = Float(gets) rescue nil
chosen_number_1 = error_message_numbers(chosen_number_1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to my suggestion above, it may be interesting to refactor to a name like get_number, and that method could change to

def error_message_numbers
  input = Float(gets) rescue nil
  while input == nil
    puts "Oops! Your input was invalid. Please enter a number: "
    input = Float(gets) rescue nil
  end
  return input
end


print "\nPlease enter your second number: "

chosen_number_2 = Float(gets) rescue nil
chosen_number_2 = error_message_numbers(chosen_number_2)

# Call the selected operator method with the user's selected number arguments
# Store the resulting value as the answer
# Note: operators are methods (send reads the given string as a method)
answer = chosen_number_1.send(operators[:"#{chosen_operator}"], chosen_number_2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this solution RULES! In practicality, I hope that you are never in a work situation where you will realistically need to use .send, because that's a dangerous method! But for this specific case, it ends up being perfect. Well done, and nice work combining it with your operators lookup hash.


# Adjust answer appropriately if user is dividing by 0
if operators[:"#{chosen_operator}"] == "/" && chosen_number_2 == 0
answer = "infinity"
end

# Print the resulting formula and outcome
puts "#{chosen_number_1} #{operators[:"#{chosen_operator}"]} #{chosen_number_2} = #{answer}"
puts "The answer is #{answer}!"

puts "\nThank you for using the calculator!"