Skip to content

Conversation

@rinostar
Copy link

@rinostar rinostar commented Aug 7, 2019

Calculator

Congratulations! You're submitting your assignment.

Comprehension Questions

Question Answer
What went well in your code style, such as indentation, spacing, variable names, readability, etc.? What was lacking? I tried to keep the code short by using three methods.
How did your code keep track of user input? The program gets the input, evaluate through methods, and store input under variable "result1", "result2", "result3".
How did your code determine what operation to perform? Based on user input for the operator, a method determine which operation to perform.
What opportunities exist to create small methods for this project? since we are repeatedly evaluating/validating user inputs, small methods will save codes; I would like to figure out a method to get user inputs if time allows.
In the next project, what would you change about your process? What would you keep doing? I don't like how my main program session is - it seems too complicated under one if...elsif..else condition. Would spend more time to optimize this session next time.

Copy link

@beccaelenzil beccaelenzil left a comment

Choose a reason for hiding this comment

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

Your code meets the required specifications and you have used a clever method to check for valid user input. The user experience when there is valid user input is quite refined. A few things to consider:

  • Use meaningful variable names to make your code more readable/understandable
  • Avoid repeating code (DRY) by using methods and loops
  • Use when/case and if/elsif/else to simplify complicated data structures
  • Use while or until to continually check for valid user input rather than exiting the program when you receive invalid input.
    See specific comments in the code review.

@@ -0,0 +1,55 @@
# 1st method - evaluate_oper(operator): evaluate user input to see if it is one of the four avaliable operators (either symbol or word) for this calculator program. If yes, method will return the input; if no, method will display error message. Assumption: only lowercase input
def evaluate_oper(operator)
if ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"].include?(operator)

Choose a reason for hiding this comment

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

Consider using a while loop to continually ask for user input until it is valid.

end
end
# 2nd method - evaluate_num(number): evaluate user input to see if it is a number. Prior - all input are strings; Post - all input are converted into float [number will remain float; letter will become 0; expect for integer 0]. Assumption: don't need to handle 0 for this assignment *** TA Kaida helped me on this method
def evaluate_num(number)

Choose a reason for hiding this comment

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

Similar to evaluate_op, consider using a while loop to continually ask for user input until it is valid.

end
end
# 3rd method - operation(a,b,c): take variable a to compare against the four available operators and return corresponding calculation of numbers b and c. Assumption: input for a is one of the four available operators
def operation(a,b,c)

Choose a reason for hiding this comment

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

Consider using a case/when block to simplify your code.

puts "Weclome to my calculator!\nPlease choose one operator from below (either name or symbol;lowercase only please).\n1. add(+)\n2. substract(-)\n3. multiply(*)\n4. divide(/) \n"
puts "What is your chosen operator?"
input1 = gets.chomp
result1 = evaluate_oper(input1)

Choose a reason for hiding this comment

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

It is advisable to use meaningful variable names. Instead of result1, result2, result3, how about operation, num1, and num2?

input1 = gets.chomp
result1 = evaluate_oper(input1)
if result1 == "This is an invalid input"
puts "Invalid input. Bye Bye"

Choose a reason for hiding this comment

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

You repeat this exact line (puts "Invalid input. Bye bye") many times. Consider how you could use methods and loops to DRY up your code.

result2 = evaluate_num(input2)
if result2 == "This is an invalid input"
puts "Invalid input. Bye Bye"
else

Choose a reason for hiding this comment

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

This conditional control structure has many nested layer. Could you simplify your code with if/elif/else or with the use of methods?

@beccaelenzil
Copy link

Calculator

What We're Looking For

Feature Feedback
Readable code with consistent indentation
Practices using variables appropriately
Practices using conditionals appropriately
If any, practices iteration appropriately
If any, practices using custom methods appropriately
Takes in two numbers and an operator and can perform addition
Takes in two numbers and an operator and can perform subtraction
The program handles divide when attempting to divide by zero

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