Samantha Berk - Calculator - Octos#17
Conversation
CalculatorWhat We're Looking For
Great job overall! I have a couple of inline comments below that you should check out, but in general I am quite happy with this submission. Keep up the hard work! |
| # Methods for each operator | ||
| def add(num1, num2) | ||
| result = num1 + num2 | ||
| checkForDecimal(result) |
There was a problem hiding this comment.
You call checkForDecimal in each of these methods - is there somewhere else that you could put this so you only have to write it once?
There was a problem hiding this comment.
I looked through it with one of the tutors, and we couldn't think a way to write it only once, but I was able to get it down to two times. I moved checkForDecimal and put it under getUserNumber in lines 61 and 65 on my newly committed code.
| def getFirstUserNum | ||
| begin | ||
| return Float(gets.chomp) | ||
| rescue |
There was a problem hiding this comment.
Good work figuring out the begin/rescue syntax. We'll talk more about what this is in a couple weeks.
| def getSecondUserNum | ||
| begin | ||
| return Float(gets.chomp) | ||
| rescue |
There was a problem hiding this comment.
This method looks exactly the same as getFirstUserNum. This side-steps the big advantage that methods give us: we can use the same procedure to handle different data. Instead of defining two methods, you should be able to define one, maybe called getUserNumber, and use it in both places.
| # If user is dividing numbers, prompt them for different numbers if they attempt to divide something by 0 | ||
| if (operator == "/" || operator == "divide") && num2 == 0 | ||
| puts "Sorry, you can't divide a number by 0. Please re-type the numbers you would like to divide: " | ||
| num1 = getFirstUserNum |
There was a problem hiding this comment.
This should be a while loop - if the user enters 0 for the denominator again, then the program won't catch it.
Moved checkForDecimal method, condensed getUserNumber method, and changed if statement for dividing by 0 to a while loop to keep prompting user for a valid input
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions