ruby show #{} when nothing is entered - ruby

print"Do you want to calculate your invoice? Please enter Yes(Y) or No(N)... "
calculate_cont = ""
while calculate_cont = gets.chomp()
calculate_cont.to_s
calculate_cont.upcase!
case calculate_cont
when "Y", "YES"
<calculations>
print "Do you wish to continue? "
when "N", "NO"
puts "Thank you! Press Enter to exit!"
gets()
exit()
else
puts "Error: #{calculate_cont} is not a valid entry!
puts "Please Enter a valid response! Yes(Y) or No(N)"
print "Do you wish to continue? "
end
end
when the user enter anything else other than YES or NO it will show the text that he input using #{calculate_cont}. How can i tell the user that he enters "Nothing" if the user just hit enter using #{calculate_cont}? can it be done?
Thanks

Yes, if you are calling gets.chomp and the user entered no value, the output will be an empty string. Just put another conditional
DO SOMETHING if calculate_cont.empty?

i created this method:
methods
def output(calculate_cont)
if calculate_cont == ""
return "Nothing"
else
return calculate_cont
end
end
with this output..
print"Do you want to calculate your invoice? Please enter Yes(Y) or No(N)... "
calculate_cont = ""
while calculate_cont = gets.chomp()
calculate_cont.to_s
calculate_cont.upcase!
case calculate_cont
when "Y", "YES"
<calculations>
print "Do you wish to continue? "
when "N", "NO"
puts "Thank you! Press Enter to exit!"
gets()
exit()
else
system("cls")
puts ""
puts "Error: #{output(calculate_cont)} is not a valid entry!"
puts "Please Enter a valid response! Yes(Y) or No(N)"
print "Do you wish to continue? "
end
end
this way i can show the user what he typed. :) many thanks

Related

Enter with no input as a valid boolean

I'm writing some very simple code, asking for confirmation on a text input, and
what I want to do is that if the users simply presses "Enter", make it count as a "yes". For example:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y' || ''
other_method
else
method
end
end
But when I run it on Ruby, I get the "Warning, literal string in condition", and whatever you enter, calls the "other_method". The solution I found is the following:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y'
other_method
elsif correct == ''
other_method
else
method
end
end
But it's pretty annoying, I'd rather understand why the first one doesn't work, and how can I make it work using the | |
Thank you!
What the error is saying is that you are supplying a string (literal) inside of a conditional statement by itself. When you do if correct == "y" || "" you're actually telling it if correct == "y" OR "" and just supplying the string by itself is not a condition.
To fix this you'd simply supply the condition after the operator as well as before it. Ruby does not assume you want the same thing to happen after the ||.
Like this:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y' || correct == ''
other_method
else
method
end
end
Hope this helps. Happy coding
The solution here is to use Ruby's very versatile case statement to set up a number of "cases" you want to test:
puts "you entered '#{#text}', is it correct?"
case (gets.chomp)
when 'y', 'yes', ''
method_a
else
method_b
end
This can be extended to use regular expressions for even more versatility:
case (gets.chomp)
when /\A\s*y(?:es)?\s*\z/i
method_a
else
method_b
end
Where now anything like "y" or "yes" or "Yes " will work.
When you have bunch of if statements all testing the same variable, consider using a case statement to simplify your logic.
Here is another option using Regex (Docs):
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if /^y?$/ =~ correct # This will match 'y' and empty string both
other_method
else
method
end

Accept multiple input possibilities for a single conditional branch

I have this code:
choice = $stdin.gets.chomp
if choice == "yes"
puts "ok sure"
else
puts "try again"
end
I have more than one possibility of user input to put "ok sure". That is, if I input "ok", it should still say "ok sure".
Use case construction.
case choice
when "yes", "ok"
puts "ok sure"
else
puts "try again"
end
Easiest way would be to use Array#include? like so:
choice = $stdin.gets.chomp
if ["yes", "ok"].include? choice
puts "ok sure"
else
puts "try again"
end

Ruby While loop not working correctly

I have been trying to make a while loop in ruby that responds to user input.
What is supposed to happen is that when a user inputs the word "Omega" correctly it ends the loop and displays a message, if the word "hint" is entered a message is displayed and the loop repeats, and anything else will have a "try again" message displayed. What is happening is that regardless of what is entered, the loop just asks the original question.
Can anyone find what is wrong with my code, thank you
#GateOneLocked = true
GateOnePassword = String.new('Omega')
GateOneHint= String.new('hint')
#Omega is supposed to be the correct password
while (#GateOneLocked == true) do
puts 'What is the password?'
passwordEntered = gets.to_s
if (#passwordEntered == #GateOnePassword)
#GateOneLocked == false
else if (#passwordEntered != #GateOneHint)
puts "This is a hint: the password is 'Omega'"
puts " "
puts " "
else
puts "wrong password, try again"
puts " "
puts " "
end
end
end
puts 'You entered the correct password!'
instead #GateOneLocked == false should be #GateOneLocked = false
addiotional remarks:
in ruby variable names follow different conventions than C#/Java, instead of
GateOneLocked devs write gate_one_locked
no need to write String.new, variable = "your_string" is enough (GateOnePassword = 'Omega')
while(#GateOneLocked) is enough - no need to check whether it's == true
I made several fixes to your code. I changed the else if to elsif and added # to the variable references that were missing it so that your code is consistent. (I'd actually ditch them all unless you're using this code in a class.) I changed your #GateOneLocked == false to use the assignment operator instead of the comparison operator. Most importantly, perhaps, I added a chomp call that will remove the \n (new line character) from your user's input. Also, I changed the comparison in the elsif to == so that your user can request a hint, which is what I think you intended.
#GateOneLocked = true
#GateOnePassword = String.new('Omega')
#GateOneHint= String.new('hint')
#Omega is supposed to be the correct password
while (#GateOneLocked == true) do
puts 'What is the password?'
#passwordEntered = gets.chomp.to_s
if (#passwordEntered == #GateOnePassword)
puts "abc"
#GateOneLocked = false
elsif (#passwordEntered == #GateOneHint)
puts "This is a hint: the password is 'Omega'"
puts " "
puts " "
else
puts "wrong password, try again"
puts " "
puts " "
end
end
puts 'You entered the correct password!'

How to wrap a loop around an if statement [duplicate]

This question already has answers here:
Is there a "do ... while" loop in Ruby?
(10 answers)
Closed 8 years ago.
I'm coding a program that asks whether or not the user wants to give his name. If the user responds 'yes', the question is asked; on 'no' the program quits. If the users enter anything else, they are reminded to say either 'yes' or 'no'.
My code so far:
puts "Would you like to give us your name? (type yes or no)"
answer = gets.chomp
if answer == "yes"
print "What's your name?"
name = gets.chomp
puts "Nice to meet you, #{name}"
elsif answer == "no"
puts "Oh, ok. Good bye"
else
puts "You need to answer yes or no"
end
I need start over, if the user does not enter 'yes' or 'no' for the initial question.
You can solve that problem with a while loop, that breaks only when the correct input is made.
puts "Would you like to give us your name? (type yes or no)"
while answer = gets.chomp
case answer
when "yes"
print "What's your name?"
name = gets.chomp
puts "Nice to meet you, #{name}"
break
when "no"
puts "Oh, ok. Good bye"
break
else
puts "You need to answer yes or no"
end
end
answer = ""
while (answer != "yes" && answer != "no") do
puts "Would you like to give us your name? (type yes or no)"
answer = gets.chomp
end
if answer == "yes"
print "What's your name?"
name = gets.chomp
puts "Nice to meet you, #{name}"
elsif answer == "no"
puts "Oh, ok. Good bye"
else
puts "You need to answer yes or no"
end
This would be better accomplished creating a Method
Something like this will work for you:
def getname
# ask the user if we should continue
puts "Would you like to give us your name? (type yes or no)"
answer = gets.chomp
if answer == "yes"
# the user said yes. get the name
print "What's your name?"
name = gets.chomp
elsif answer == "no"
# the user said no. get out of here
puts "Oh, ok. Good bye"
else
# the user didnt answer correctly
puts "You need to answer yest or no"
# so we call this very function again
getname
end
end
# call the above method that we created
getname
What we did here was wrap your code in a method declaration. In that very method declaration we call that very method if the user doesnt supply the expected input.
Hope this helps.

Variables in if/else statement won't work

I'm creating an interactive story, not a game. The options don't work in the if/else; here is the code.
puts "Choose (1)yes or (2)no"
choice = gets.chomp
if #{choice}==1
puts "you chose yes"
elsif #{choice}==2
puts "you chose no"
else
puts "Invalid Choice"
I tried leaving it intro, but that just calls the else statement, and with this setup, the tic tac toe guy in front of the brackets, it always calls the first option. please help.
if #{choice}==1 isn't how you test the value of variables. #{} is for string interpolation; you don't have to interpolate anything. Just use if choice == "1". Note that, because you're reading a string from the console, you need to compare against "1", not 1.
puts "Choose (1)yes or (2)no"
choice = gets.chomp
if choice == "1"
puts "you chose yes"
elsif choice == "2"
puts "you chose no"
else
puts "Invalid Choice"
end

Resources