-
Notifications
You must be signed in to change notification settings - Fork 42
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
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,59 @@ | ||
def r_or_z(n) | ||
if n.include?(".") | ||
return n.to_f | ||
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 | ||
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 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) | ||
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. Watch your spacing here. Good style is usually to put a space on either side of an equals sign or operator, like |
||
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" |
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.
This is a clever way to figure out what's needed here.