Skip to content
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

pipes-Mariana-Calculator.rb #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions pipes-Mariana-Calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
def r_or_z(n)
if n.include?(".")
return n.to_f
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a clever way to figure out what's needed here.

else
return n.to_i
end
end


puts "Welcome to the fun calculator"



puts "Please provide the name or symbol of the operation that you would like to do"
operation=gets.chomp



puts "Please provide the first number."
puts "The operation will be made at the order that you entered"
n1=gets.chomp
n1=r_or_z(n1)


puts "Provide the second number."
n2=gets.chomp
n2=r_or_z(n2)

symbol="unvalid"

while symbol !="valid" do
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like the idea of using a variable to track whether the loop has to restart. As long as there's only two possibilities (valid and invalid), it's common to use a boolean in this role, something like:

valid_symbol = false

while !valid_symbol do
  # ...


if operation == "+" || operation == "add"
puts "#{n1} + #{n2} = #{n1+n2}"
symbol ="valid"
elsif operation == "-" || operation == "subtract"
puts "#{n1} - #{n2} = #{n1-n2}"
symbol ="valid"
elsif operation == "*" || operation == "multiply"
puts "#{n1} * #{n2} = #{n1*n2}"
symbol ="valid"
elsif operation == "/" || operation == "divide"
while n2==0 do
puts "It is no possible to divide by 0. Please enter a valid number"
n2=gets.chomp
n2=r_or_z(n2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Watch your spacing here. Good style is usually to put a space on either side of an equals sign or operator, like n2 = gets.chomp

end
puts "#{n1} / #{n2} = #{n1.to_f/n2}"
symbol ="valid"
else
puts "Sorry, you entered an invalid operation."
puts "Please provide the name or symbol of the operation that you would like to do"
symbol ="unvalid"
operation=gets.chomp
end

end

puts "Thanks for using fun calculator"