Using Loops for prompts with If/Else/Esif - ruby

I started with:
puts "Hello there, and what's your favorite number?"
favnum = gets.to_i
puts "Your favorite number is #{favnum}?" " A better favorite number is #{favnum + 1}!"
puts "Now, what's your favorite number greater than 10?"
favnumOverTen = gets.to_i
if favnumOverTen < 10
puts "Hey! I said GREATER than 10! Try again buddy."
else
puts "Your favorite number great than 10 is #{favnumOverTen}?"
puts "A bigger and better number over 10 is #{favnumOverTen * 10}!"
puts "It's literally 10 times better!"
end
That worked fine, but if the user entered a number less than 10 the program ended.
I want the user to be prompted to try again until they enter a number greater than 10.
Am I supposed to do that with a loop?
Here's what I took a swing at, but clearly it's wrong:
puts "Hello there, and what's your favorite number?"
favnum = gets.to_i
puts "Your favorite number is #{favnum}?" " A better favorite number is #{favnum + 1}!"
puts "Now, what's your favorite number greater than 10?"
favnumOverTen = gets.to_i
if favnumOverTen < 10
loop.do
puts "Hey! I said GREATER than 10! Try again buddy."
favnumOverTen = gets.to_i
until favnumOverTen > 10
else
puts "Your favorite number great than 10 is #{favnumOverTen}?"
puts "A bigger and better number over 10 is #{favnumOverTen * 10}!"
puts "It's literally 10 times better!"
end

Here's a way that's a little shorter than the previous two:
puts "Now, what's your favorite number greater than 10?"
until (favnumOverTen = gets.to_i) > 10
puts "Hey! I said GREATER than 10! try again buddy."
end
This works because assignment returns the value assigned to the variable.

A better solution would be to get the favorite number of 10 once, then start an until loop checking for favnumOverTen > 10 like this:
puts "What is your favorite number greater than 10?"
favnumOverTen = gets.to_i
until favnumOverTen > 10 do
puts "Hey! I said GREATER than 10! Try again buddy."
favnumOverTen = gets.to_i
end
This way, if the initial entry is greater than 10, the until loop is never executed (saves you the if statement).
Also, just so you're aware, it's not considered idiomatic Ruby to write variable names using camel case (i.e. favnumOverTen should be favnum_over_ten).

Related

Ruby Guessing Game w 'Loop Do'

I created a guessing game through Ruby and I believe the structure of my code is off. When entering 'Cheat', you are given the random number then asked to type it in again. When typed in again, it says the random number is not correct and always defaults to my 'elseif' in line 45.
puts "Hey! I'm Sam. What's your name?"
name = gets
puts "Welcome #{name}. Thanks for playing the guessing game.
I've chosen a number between 1-100.
You'll have 10 tries to guess the correct number.
You'll also recieve a hint when you're guess is wrong.
If you feel like being a big ol cheater, type 'Cheat'.
Let's get started..."
random_number = rand(1...100)
Cheat = random_number
counter = 10
loop do
break if counter == 0
divisor = rand(2...10)
guess = gets.chomp
break if guess.to_i == random_number
counter -= 1
if
guess == random_number
puts 'You guessed the right number! You win!'
end
if counter < 4
puts "You can go ahead and cheat by typing 'Cheat'..."
end
if guess.to_s.downcase.eql? "cheat"
puts "The random number is #{random_number} you CHEATER!! Go ahead and type it in..."
guess = gets.chomp
puts = "You win cheater!"
end
if
guess.to_i < random_number
puts 'Ah shucks, guess again!'
guess = gets.chomp
elsif
guess.to_i > random_number
puts 'Too high, guess again!'
guess = gets.chomp
end
if random_number % divisor == 0
puts "Thats not it.\n #{guess} is #{guess.to_i > random_number ? 'less' : 'greater'} than the random number.
The random number is divisible by #{divisor}.\nTry again: "
elsif
puts "That's not the random number.\n #{guess} is #{guess.to_i > random_number ? 'less' : 'greater'} than the random number.
The random number is NOT divisible by #{divisor}.\nTry again: "
end
end
if counter > 0
puts "The number is #{random_number}! You win!"
else
puts "You lose! Better luck another time."
end
this is the response i get in the terminal
Let's get started...
Cheat
The random number is 96 you CHEATER!! Go ahead and type it in...
96
Thats not it.
96 is greater than the random number.
The random number is divisible by 8.
Try again:
The problem is here:
puts = "You win cheater!"
You're assigning the string "You win cheater!" to a local variable named puts. Changing it to this fixes the problem:
puts "You win cheater!"
You'll probably also want to put a break after that line.
As an aside, this pattern:
loop do
break if counter == 0
# ...
end
...would be better expressed as:
while counter > 0
# ...
end
...or:
until counter == 0
# ...
end
Also, you should always put the condition for an if/elsif/whathaveyou on the same line as if et al. Why? Because if you don't you get bugs like this:
if random_number % divisor == 0
# ...
elsif
puts "..."
end
Can you spot the bug? You forgot to put a condition after elsif, or used elsif when you meant to use else, which means that the return value of puts (which is always nil) is being used as the condition, just as if you had written elsif puts "...".
If you make a habit of always putting the condition on the same line as if/elsif, your eye will get used to it and errors like this will jump out at you.

in ruby Create a program that asks a user for 10 numbers and then returns the sum

puts "Im gonna ask you for 10 numbers and then give you the sum"
puts "Give me the first number"
first_number= gets.chomp
puts "Give me the second number"
second_number=gets.chomp
puts "Give me the third number "
third_number=gets.chomp
puts "Give me the fourth number"
fourth_number= gets.chomp
puts "Give me the fifth number"
fifth_number=gets.chomp
puts "Give me the sixth number"
sixth_number=gets.chomp
puts "Give me the seventh number"
seventh_number=gets.chomp
puts "Give me the eighth number"
eighth_number=gets.chomp
puts "Give me the ninth number"
ninth_number=gets.chomp
puts "Give me the tenth number"
tenth_number= gets.chomp
puts "The Sum of all your TEN numbers is:"
puts first_number.to_i + second_number.to_i+ third_number.to_i + fourth_number.to_i + fifth_number.to_i + sixth_number.to_i+ seventh_number.to_i+ eighth_number.to_i+ ninth_number.to_i + tenth_number.to_i
sum = 10.times.inject(0) do|sum,i|
puts "Enter #{i+1}th number"
sum = sum + gets.chomp.to_f
end
puts "The sum is #{sum}"
This example works well with decimal and integer you pass.
You are missing the point that, you are a software writer. You write softwares for end-users and its up to the users, how many numbers they want to enter. So I suggest you to learn writing dynamic softwares. Do not just hard-code everything. Make things flexible so that its easily tweak-able.
One idea is, you can even ask user how many numbers they want to enter.

How to keep program from closing?

I'm using a Ruby interpreter to run the code I created(a simple guess a number 1-100 code), but every time you guess the number correctly or incorrectly after x number of times it automatically closes itself after it prints out "You Win!" or "You Lose!". Here's the code:
srand
random_number = rand 1..100
guesses = 10
while guesses > 0
puts "I'm thinking of a number between 1 and 100."
print "What number am I thinking of?"
guess = gets.chomp.to_i
guesses -= 1
break if guess == random_number
puts "Too high" if guess > random_number
puts "Too low" if guess < random_number
end
if guess == random_number
puts "You win!"
else
puts "You lose, sorry!"
end
How can I keep it from shutting itself down, so the user can see the displayed message?
Why not end with:
puts "Hit enter to exit."
gets

rounding off to the nearest number ruby

def percent_more
puts "What is the biggest number?"
biggest_number = gets.chomp
puts "What is the smallest number?"
smallest_number = gets.chomp
difference = biggest_number.to_i - smallest_number.to_i
total_percent_more = difference / smallest_number.to_f
puts "Your biggest number is #{total_percent_more}% bigger then your smallest number. Don't forget to round off to the nearest whole percent!"
end
Now that code will tell you what percent more biggest_number is than smallest_number. But the problem is it prints out a long list of decimals, which are a pain to sort through. So if I wanted the code to only show say the first 3 numbers what would I do??
What you want to use is total_percent_more.round like so:
puts "What is the biggest number?"
biggest_number = gets.chomp
puts "What is the smallest number?"
smallest_number = gets.chomp
difference = biggest_number.to_i - smallest_number.to_i
total_percent_more = difference / smallest_number.to_f
puts "Your biggest number is #{total_percent_more.round}% bigger then your smallest number. Don't forget to round off to the nearest whole percent!"
See the docs for more info :
http://www.ruby-doc.org/core-2.1.2/Float.html#method-i-round
in ruby versions earlier than 1.9 you'll need to use sprintf like so:
puts "Your biggest number is #{sprintf('%.2f', total_percent_more)}% bigger then your smallest number. Don't forget to round off to the nearest whole percent!"
You can change the amount of decimal places by changing the number.
See docs for more details:
http://www.ruby-doc.org/core-1.8.7/Kernel.html#method-i-sprintf
result = 10/6.0
puts result
printf("%.3f\n", result)
--output:--
1.66666666666667
1.667
Here is an example how to round to 2 decimal places
amount = 342
puts amount.round(2)
If you wanted to round to the nearest 3 decimal places then something like:
puts amount.round(3)

Ruby gets/puts only for strings?

I'm new to Ruby and am currently working on some practice code which looks like the following:
puts 'Hello there, Can you tell me your favourite number?'
num = gets.chomp
puts 'Your favourite number is ' + num + '?'
puts 'Well its not bad but ' + num * 10 + ' is literally 10 times better!'
This code however just puts ten copies of the num variable and doesn't actually multiply the number so I assume I need to make the 'num' variable an integer? I've had no success with this so can anyone show me where I'm going wrong please?
If you are using to_i, then chomp before that is redundant. So you can do:
puts 'Hello there, Can you tell me your favourite number?'
num = gets.to_i
puts 'Your favourite number is ' + num.to_s + '?'
puts 'Well its not bad but ' + (num * 10).to_s + ' is literally 10 times better!'
But generally, using "#{}" is better since you do not have to care about to_s, and it runs faster, and is easier to see. The method String#+ is particularly very slow.
puts 'Hello there, Can you tell me your favourite number?'
num = gets.to_i
puts "Your favourite number is #{num}?"
puts "Well its not bad but #{num * 10} is literally 10 times better!"
Use the to_i method to convert it to an integer. In other words, change this:
num = gets.chomp
To this:
num = gets.chomp.to_i
you can also make sure the number that the user is using is an integer this way:
num = Integer(gets.chomp)
but you have to create a way to catch the error in case the user input otherwise like a char, or string so; it is must better to use:
num = gets.chomp.to_i
In case the user put another type of data, num will be equal to 0 like you can see in this test example:
puts "give me a number:"
num = gets.chomp.to_i
if num >3
puts "#{num} es mayor a 3 "
else
puts "#{num} es menor a 3 o 3"
end
This a example of the interaction with that script:
give me a number:
sggd
0 es menor a 3 o 3
nil
I hope this clarify better your point.
I wrote a similar program as yours. Here is how I finally got it to work properly! I had to assign the favorite number to be an integer. Then, in the next line I set the new_fav_num with the value of fav_num +1 and then converted it to string. After that, you can just plug your code into the return statement that you want to say to the user, only you have to convert the first fav_num to a string.
puts "What is your favorite number?"
fav_num = gets.chomp.to_i
new_fav_num = (fav_num + 1).to_s
puts "Your favorite number is " + fav_num.to_s + ". That's not bad, but " +
new_fav_num + " is bigger and better!"
Hope this helps.

Resources