1 - print "Welcome to the calculator"?
2 - Get first number from the user
3 - Get second number from the user
4 - Ask which operation the user wants to perform
5 - Show the results
| # Calculate based on user input | |
| def calculate(number1, number2, operation) | |
| result = case operation | |
| when "-" then number1 - number2 | |
| when "+" then number1 + number2 | |
| when "*" then number1 * number2 | |
| when "/" then number1 / number2 | |
| else | |
| result = "not valid" | |
| end | |
| return result | |
| end |
| require_relative "calculator.rb" | |
| other_operation = "y" | |
| until other_operation == "n" | |
| puts "This is a calculator. Input 2 numbers" | |
| puts "1) Insert the first number" | |
| number1 = gets.chomp.to_f | |
| puts "2) Insert the second number" | |
| number2 = gets.chomp.to_f | |
| # # Assert + - / * | |
| puts "3) What kind of operation? \nChoose one: + - / *" | |
| operation = gets.chomp # Getting input from the user | |
| # Showing what the user inputed | |
| result = calculate(number1, number2, operation) | |
| puts "Oh, the result is #{result}" | |
| puts "Want to do another one? Type (n/y)" | |
| other_operation = gets.chomp | |
| # You can use REGEX! We are expecting N OR Y (the PIPE | is for saying OR) | |
| #unless other_operation == "y" or other_operation == "n" | |
| # ANY LETTER BUT N OR Y we break | |
| unless other_operation =~ /^n|y$/ | |
| puts "This is not an option" | |
| break | |
| end | |
| end | |
| # if condition == "-" | |
| # elsif condition == "+" | |
| # elsif condition == "*" | |
| # elsif condition == "/" | |
| # else | |
| # end |