-
Notifications
You must be signed in to change notification settings - Fork 48
Lindsay - Calculator - Edges #46
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,63 @@ | ||
| #Welcome user and accept numbers to be evaluated. Validate user input. | ||
| puts "Welcome to the calculator program!" | ||
| puts "You will need to enter two numbers to be evaluated." | ||
| puts "Please enter the first." | ||
| begin | ||
| number_1 = gets.chomp | ||
| number_1 = Float(number_1) | ||
| rescue | ||
| puts "There is an error with your number. Please make sure you're using numbers/decimals only and try again." | ||
| retry | ||
| end | ||
|
|
||
| puts "Please enter another number." | ||
| begin | ||
| number_2 = gets.chomp | ||
| number_2 = Float(number_2) | ||
| rescue | ||
| puts "There is an error with your number. Please make sure you're using numbers/decimals only and try again." | ||
| retry | ||
| end | ||
|
|
||
| #create a method for each operator | ||
| def add_two_numbers(first_number, second_number) | ||
| puts "#{first_number} + #{second_number} = #{first_number + second_number}" | ||
| end | ||
|
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. These methods all |
||
|
|
||
| def subtract_two_numbers(first_number, second_number) | ||
| puts "#{first_number} - #{second_number} = #{first_number - second_number}" | ||
| end | ||
|
|
||
| def multiply_two_numbers(first_number, second_number) | ||
| puts "#{first_number} * #{second_number} = #{first_number * second_number}" | ||
| end | ||
|
|
||
| def divide_two_numbers(first_number, second_number) | ||
| if second_number == 0 | ||
| puts "#{first_number} / #{second_number} = According to Wikipedia, the result of any number divided by zero is undefined." | ||
| else | ||
|
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. I like that you've included the division-specific error handling code in the division method. This feels like good organization to me. |
||
| puts "#{first_number} / #{second_number} = #{first_number / second_number}" | ||
| end | ||
| end | ||
|
|
||
| #prompt the user to choose their operator | ||
| puts "Choose your operator! Type the name or symbol from the list below:" | ||
| puts "1. add (+)" | ||
| puts "2. subtract (-)" | ||
| puts "3. multiply (*)" | ||
| puts "4. divide (/)" | ||
| operator = gets.chomp | ||
|
|
||
| #use the appropriate method to evaluate the values | ||
| case operator | ||
| when "add", "+" | ||
| add_two_numbers(number_1, number_2) | ||
| when "subtract", "-" | ||
|
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 code isn't repeated, but I think it would still increase readability to wrap this |
||
| subtract_two_numbers(number_1, number_2) | ||
| when "multiply", "*" | ||
| multiply_two_numbers(number_1, number_2) | ||
| when "divide", "/" | ||
| divide_two_numbers(number_1, number_2) | ||
| else | ||
| puts "I'm sorry. That is not a valid option." | ||
| end | ||
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.
You've written almost exactly the same code here twice, to get the first number and the second number. Could you DRY that up by putting this logic in a method?