Here's part of my code, the first part of the ˚ statement works, but I would like a separate message to appear for a non-numerical input such as "$boo", "ten", "idk").
puts "Candy Sold: "
candy_sold = gets.chomp.to_i
until candy_sold >= 0
if candy_sold < 0
puts "Please enter a positive number:"
candy_sold = gets.chomp.to_i
elsif candy_sold.class == String # This is where the issue is
puts "Please enter a positive number:"
candy_sold = gets.chomp.to_i
end
end
If I am understanding your question correct, you just want to allow only positive numbers. In this case, you can use the cover? method with a range.
puts "Candy Sold: "
candy_sold = gets.chomp
until ('0'..'9').cover? candy_sold
puts "Please enter a positive number"
candy_sold = gets.chomp
end
Candy Sold:
> Hello
=> Please enter a positive number
> -97
=> Please enter a positive number
> -0
=> Please enter a positive number
> 1
=> nil
If you check already converted number there is no way to tell if it was initially valid format or not. You have to compare with initial input somehow.
One of the solutions:
ask_for_amount = true
while ask_for_amount
candy_number_input = gets.chomp.strip
candy_sold = candy_number_input.to_i
if candy_number_input != candy_sold.to_s
puts "Please enter a valid positive number:"
elsif candy_sold < 0
puts "Please enter a positive number:"
else
ask_for_amount = false
end
end
Related
I created a calculator in ruby. I am wondering how to put this in a loop so I don't have to run it constantly. I am new to programming so please understand I am I just trying to learn. I would appreciate any help provided.
puts "Hello, My name is Calvin The Calculator and I am a calculator that can do basic functions such as Adding, Subtracting, Multiplying and Dividing"
puts "Press a and enter to enable my services"
enable = gets.chomp
if enable == "a"
puts "Choose which operation you want to do. + for adding, - for subtraction, * for multiplication and / for division"
else
"Puts Im Waiting..."
end
which_operation = gets.chomp
if which_operation == "+"
puts "What is the first number you want to add"
adding_first_number = gets.chomp.to_i
puts "What is the second number you want to add to #{adding_first_number}"
adding_second_number = gets.chomp.to_i
puts "#{adding_first_number} + #{adding_second_number} is #{adding_first_number + adding_second_number}"
else
end
if which_operation == "-"
puts "What is the first number you want to subtract"
subtracting_first_number = gets.chomp.to_i
puts "What is the number you want to subtract from #{subtracting_first_number}"
subtracting_second_number = gets.chomp.to_i
puts "#{subtracting_first_number} - #{subtracting_second_number} is #{subtracting_first_number - subtracting_second_number}"
else
end
if which_operation == "*"
puts "What is the first number you want to multiple"
multiplying_first_number = gets.chomp.to_i
puts "What is the number you want to multiple #{multiplying_first_number} by"
multiplying_second_number = gets.chomp.to_i
puts "#{multiplying_first_number} * by #{multiplying_second_number} is #{multiplying_first_number * multiplying_second_number}"
else
end
if which_operation == "/"
puts "What is the first number to your divison question?"
dividing_first_number = gets.chomp.to_i
puts "What is the divisor?"
dividing_second_number = gets.chomp.to_i
puts "#{dividing_first_number} divided by #{dividing_second_number} is #{dividing_first_number / dividing_second_number}"
else
end
For example:
until (which_operation = gets.chomp).empty?
if which_operation == "+"
...
end
if which_operation == "-"
...
end
if which_operation == "*"
...
end
if which_operation == "/"
...
end
end
This loop will work until you press Enter without entering any text before it.
P.S.: Better use case operator instead of multiple if.
P.P.S.: All your code, after loop addition and without case operator, will be:
puts "Hello, My name is Calvin The Calculator and I am a calculator that can do basic functions such as Adding, Subtracting, Multiplying and Dividing"
puts "Press a and enter to enable my services"
until gets.chomp == "a"
puts "I'm Waiting..."
end
puts "Choose which operation you want to do. + for adding, - for subtraction, * for multiplication and / for division"
until (which_operation = gets.chomp).empty?
if which_operation == "+"
puts "What is the first number you want to add"
adding_first_number = gets.chomp.to_i
puts "What is the second number you want to add to #{adding_first_number}"
adding_second_number = gets.chomp.to_i
puts "#{adding_first_number} + #{adding_second_number} is #{adding_first_number + adding_second_number}"
elsif which_operation == "-"
puts "What is the first number you want to subtract"
subtracting_first_number = gets.chomp.to_i
puts "What is the number you want to subtract from #{subtracting_first_number}"
subtracting_second_number = gets.chomp.to_i
puts "#{subtracting_first_number} - #{subtracting_second_number} is #{subtracting_first_number - subtracting_second_number}"
elsif which_operation == "*"
puts "What is the first number you want to multiple"
multiplying_first_number = gets.chomp.to_i
puts "What is the number you want to multiple #{multiplying_first_number} by"
multiplying_second_number = gets.chomp.to_i
puts "#{multiplying_first_number} * by #{multiplying_second_number} is #{multiplying_first_number * multiplying_second_number}"
elsif which_operation == "/"
puts "What is the first number to your divison question?"
dividing_first_number = gets.chomp.to_i
puts "What is the divisor?"
dividing_second_number = gets.chomp.to_i
puts "#{dividing_first_number} divided by #{dividing_second_number} is #{dividing_first_number / dividing_second_number}"
end
puts "\nLet's try again: "
end
print "Enter your age in Numbers:"
user_input = gets.chomp
if user_input > 21 && user_input < 30
puts "XYZ"
elsif user_input > 31 && user_input < 40
puts "YZX"
elsif user_input > 40
puts "ZXY"
else
puts "Golden Age!"
end
The problem is that gets.chomp returns a string
You can convert it to integer by using String#to_i: gets.chomp.to_i
You can further improve the readability of your code by using between?:
print "Enter your age in Numbers:"
number = gets.chomp.to_i
if number.between?(22, 29)
puts "XYZ"
elsif number.between?(32, 39)
puts "YZX"
elsif number > 40
puts "ZXY"
else
puts "Golden Age!"
end
Or even better - a case statement:
print "Enter your age in Numbers:"
number = gets.chomp.to_i
case number
when (22...30) then puts "XYZ"
when (32...40) then puts "YXZ"
when (40..Float::INFINITY) then puts "ZXY"
else puts "Golden Age!"
end
Note: I made the ranges compatible with your original solution, but please note that there are some gaps in there (31 for example).
You can compare objects on which <=> is defined properly, but you cannot compare a string object with a fixnum object. If you intended to compare as an integer, you need to convert the input to integer. Also, (assuming that you only get inputs that can be converted to integers), your condition is redundant. And you are probably using && incorrectly. It can be written like this:
print "Enter your age in Numbers:"
user_input = gets.to_i
if user_input < 20 or [20, 21, 30, 31, 40].include?(user_input)
puts "Golden Age!"
elsif user_input < 30
puts "XYZ"
elsif user_input < 40
puts "YZX"
else
puts "ZXY"
end
I'm making this small program and I want to check if the user entered an Integer number, if he did it the program will continue but if the user enters a string I want to the program to ask the user to enter an Integer until he does it, here's my code snippet:
print "How old are you:"
user_age = gets.chomp.to_i
if user_age.is_a? String
puts "Please enter an integer number:"
user_age = gets.chomp.to_i
until user_age.is_a? Numeric
puts "Please enter an integer number:"
user_age = gets.chomp.to_i
break if user_age.is_a? Numeric
end
end
I think that your error is the to_i after gets.chomps.
to_i returns the first number(s) at the begining of a string or 0, so you allways get a number (0 or another number). Here are some examples:
2.2.1 :001 > "12".to_i
=> 12
2.2.1 :002 > "12aaa".to_i
=> 12
2.2.1 :003 > "aaa12aaa".to_i
=> 0
2.2.1 :004 > "aaaaaa".to_i
=> 0
I wrote this code, which works for me:
print "How old are you:"
begin
user_age = gets.chomp
user_age = Integer(user_age)
rescue ArgumentError
print "Please enter an integer number:"
retry
end
print user_age.to_s
begin
p "How old are you:"
user_age = Integer(gets.chomp)
rescue
p "Please enter an integer number:"
retry
end
print user_age
Maybe instead you could also use:
until user_age.to_i.to_s == user_age
puts "Please enter your age as a number"
user_age = gets.chomp
end
print "How old are you: "
user_age = gets.chomp
while true
if user_age.to_i.to_s == user_age
print "Your age is #{user_age}"
break
else
print "please enter your age in number "
user_age = gets.chomp
end
end
I trying to make loop through if statement if user input is invalid it keep telling to enter number only or break if true
I tried this is my code
class String
def numeric?
Float(self) != nil rescue false
end
end
cond = false
puts "Enter number "
line = gets.chomp.strip
while cond == false
if (line.numeric? )
puts "Ok nice "
cond = true
else
puts "Please enter number only "
end
end
but it keep looping if the condition is false just printing "Please enter number only "
i will be very happy for any suggestion
Thank you
The problem is that after telling the user to just enter a number you don't read another number, you just loop back around.
The easiest way to fix this is to move the prompt and input into the while loop, a bit like this:
class String
def numeric?
Float(self) != nil rescue false
end
end
cond = false
while cond == false
puts "Enter number "
line = gets.chomp.strip
if (line.numeric? )
puts "Ok nice "
cond = true
else
puts "Please enter number only "
end
end
try this:
while cond == false
if (line.numeric? )
puts "Ok nice "
cond = true
else
puts "Please enter number only "
line = gets.chomp.strip
end
end
Right after overriding String method, try this:
while true
print "Enter number "
line = gets.chomp.strip
if line.numeric?
puts "Ok nice"
break
else
puts "Please enter number only"
end
end
I have the following code:
def say(msg)
puts "=> #{msg}"
end
def do_math(num1, num2, operation)
case operation
when '+'
num1.to_i + num2.to_i
when '-'
num1.to_i - num2.to_i
when '*'
num1.to_i * num2.to_i
when '/'
num1.to_f / num2.to_f
end
end
say "Welcome to my calculator!"
run_calculator = 'yes'
while run_calculator == 'yes'
say "What's the first number?"
num1 = gets.chomp
say "What's the second number?"
num2 = gets.chomp
say "What would you like to do?"
say "Enter '+' for Addition, '-' for Subtraction, '*' for Multiplication, or '/' for Division"
operation = gets.chomp
if num2.to_f == 0 && operation == '/'
say "You cannot devide by 0, please enter another value!"
num2 = gets.chomp
else
result = do_math(num1, num2, operation)
end
say "#{num1} #{operation} #{num2} = #{result}"
say "Would you like to do another calculation? Yes / No?"
run_calculator = gets.chomp
if run_calculator.downcase == 'no'
say "Thanks for using my calculator!"
elsif run_calculator.downcase == 'yes'
run_calculator = 'yes'
else
until run_calculator.downcase == 'yes' || run_calculator.downcase == 'no'
say "Please enter yes or no!"
run_calculator = gets.chomp
end
end
end
I need it to take the num1 and num2 variables that the user inputs and validate that they are numbers and return a message if they aren't.
I would like to use a Regex, but I don't know if I should create a method for this or just wrap it in a loop.
The Integer method will raise an exception when the given string is not a valid number, whereas to_i will fail silently (which I think is not desired behavior):
begin
num = Integer gets.chomp
rescue ArgumentError
say "Invalid number!"
end
If you want a regex solution, this will also work (although I recommend the method above):
num = gets.chomp
unless num =~ /^\d+$/
say "Invalid number!"
end
You would often see each section written something like this:
ERR_MSG = "You must enter a non-negative integer"
def enter_non_negative_integer(instruction, error_msg)
loop do
puts instruction
str = gets.strip
return str.to_i if str =~ /^\d+$/
puts error_msg
end
end
x1 = enter_non_negative_integer("What's the first number?", ERR_MSG)
x2 = enter_non_negative_integer("What's the second number?", ERR_MSG)
Here's possible dialog:
What's the first number?
: cat
You must enter a non-negative integer
What's the first number?
: 4cat
You must enter a non-negative integer
What's the first number?
: 22
#=> 22
What's the second number?
: 51
#=> 51
x1 #=> 22
x2 #=> 51