Skip to content

Samantha Berk - Calculator - Octos#17

Open
samanthaberk wants to merge 2 commits into
Ada-C9:masterfrom
samanthaberk:master
Open

Samantha Berk - Calculator - Octos#17
samanthaberk wants to merge 2 commits into
Ada-C9:masterfrom
samanthaberk:master

Conversation

@samanthaberk

Copy link
Copy Markdown

Calculator

Congratulations! You're submitting your assignment.

Comprehension Questions

Question Answer
Describe how you stored user input in your program? I stored user input in local variables. For instance, to get the numbers the user wanted to compute, I stored the value of functions that asked the user to input integers in local variables num1 and num2 .
How did you determine what operation to perform? When I noticed that I was repeating my code in several places, I tried to refactor it by creating separate methods and then calling those methods in the program.
Do you feel like you used consistent indentation throughout your code? Yes, I used the cmd + i shortcut to double-check my indentation
If you had more time, what would you have added to or changed about the program? I would have liked to handle parenthetical inputs from the user. I think this would require accepting a string and then evaluating it.

@droberts-sea

Copy link
Copy Markdown

Calculator

What We're Looking For

Feature Feedback
Takes in two numbers and an operator and performs the mathematical operation. yes
Readable code with consistent indentation. yes

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!

Comment thread calculator.rb Outdated
# Methods for each operator
def add(num1, num2)
result = num1 + num2
checkForDecimal(result)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Comment thread calculator.rb Outdated
def getFirstUserNum
begin
return Float(gets.chomp)
rescue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good work figuring out the begin/rescue syntax. We'll talk more about what this is in a couple weeks.

Comment thread calculator.rb
def getSecondUserNum
begin
return Float(gets.chomp)
rescue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread calculator.rb Outdated
# 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants