-
Notifications
You must be signed in to change notification settings - Fork 52
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
# 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
# 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!" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this method!!