Pseudocode for a guessing game? (In Logic) - logic

I'm writing a pseudocode that describes the process of guessing a number between 1 and 100. After each guess, the player is told that the guess is too high or too low. The process continues until the player guesses the correct number. Can anyone give me any suggestions on what I should move around or remove, if any?
My code:
Start
Declarations
num GuessValue
num TRUE_VALUE = 47
output "Guess a number between 1 and 100."
input GuessValue
while GuessValue <> TRUE_VALUE
if GuessValue < TRUE_VALUE then
output "Number is too low."
elseif GuessValue > TRUE_VALUE then
output "Number is too high."
endwhile
output "Guess a number between 1 and 100."
input GuessValue
if GuessValue = TRUE_VALUE then
output "Correct!"
endif
end

Related

Ruby while loop

Writing a ruby while loop that take integers as an input and stores them in an array
when a certain integer is entered (-1) the loop should stop and print out the array.
When -1 is entered the loop stops but there is no output.
puts " Enter a number"
x = -1.to_i
number = ' '
numbers = []
while number != x
number = gets.chomp
numbers.push(number)
end
puts
numbers.pop
p numbers
The loop will never stop gets.chomp returns a String so the loop will go on for ever because:
"-1" != -1
#=> true
Try changing this to:
while number != x
number = gets.chomp.to_i
numbers.push(number)
end

Trying to figure out if user input is prime or not with ruby

I am new to Ruby and just read about methods and such I have been working on a program in order to get the user to type in a number and determine if it is prime or not. This is what I have so far.
print "Enter a number: "
num = gets.chomp
def prime(n)
factors = []
if num < 2
puts "#{num} is a prime number"
end
while n % num == 0 and num < n
factors.push(num)
num += 1
end
return factors
end
Not even sure if I'm on the right track. Really lost. Any help would be greatly appreciated!
require 'prime'
print "Enter a number: "
num = gets.chomp
Prime.prime?(n)
Or, from the scratch:
def prime?(n)
if n > 1
min_condition = Math.sqrt(n).to_i # The highest factor is necessary at most sqrt(n)
factors = 0
(1..min_condition).step(2).each do |i| # It's not necessary to check even numbers
factors += 1 if (n.to_f / i) % 1 == 0
break if factors > 1 # If the algorithm find 2 factors, the number isn't prime
end
!(factors > 1)
else
false
end
end
I know that you're doing this to learn Ruby, but keep in mind that you can just use Prime.prime? to determine whether or not a number is prime.
require 'prime'
Prime.prime?(3)
#=> true
Prime.prime?(4)
#=> false
As for your actual problem, you use both num and n inside your method, but num is defined outside, so won't be in scope. Also: you never seem to actually be calling prime.
There are some problems with your program:
You want to get the user to type in a number and determine if it is
prime or not but the output of your method prime doesn't answer
this question. It will return an array.
If a number is less then 2, it is not a prime number (wikipedia), so this piece of your code is wrong:
if num < 2
puts "#{num} is a prime number"
end
There are plenty of ways to check if a number is a prime number or not, this topic may help you to implement with Ruby.
If you don't want to use ruby base library than below code is useful for you
print "Enter a number: "
num = gets.chomp
def check_prime_number(num)
num = num.to_i
n = 1
factors = []
while (num >= n ) do
if (num % n == 0 )
factors << n unless factors.include?(n)
end
n += 1
end
if ( factors.size > 2)
puts "Factors of number #{num} :-> #{factors}"
elsif num > 0
puts "#{num} is prime"
end
end
check_prime_number(num)
Try if its helpful for you.

Why is this switch statement not working? [duplicate]

This question already has answers here:
Shortcut to make case/switch return a value
(4 answers)
Closed 6 years ago.
I have this code
(1..50).each do |num|
case num
when num % 4 == 0, num % 6 == 0
puts 'Cluck'
when num % 4 == 0
puts 'Cluck Cluck'
when num % 5 == 0
puts 'Cluck Cluck Cluck'
else
puts num
end
end
For some odd reason, instead of putting cluck cluck on the fourth line or cluck on the 24th line, it's just putting a list of 1 through 100. I can't figure out what's wrong with the switch statement. The first when using the comma or && doesn't change anything either (which I don't believe it should).
Problems
case a when b
case a
when b
tests if a is equal to b.
In your case, a is a number (num) and b is a boolean (num % 4 == 0) so this never happens.
when b,c
Another problem is that
case
when b,c
tests if b or c.
If you want to check that num is divisible by 24, you need b and c.
Solution
Remove num from case and use logical and (&&) :
(1..100).each do |num|
case
when num % 4 == 0 && num % 6 == 0
## or just :
# when num % 24 == 0
puts 'Cluck'
when num % 4 == 0
puts 'Cluck Cluck'
when num % 5 == 0
puts 'Cluck Cluck Cluck'
else
puts num
end
end
Oh well, Eric's answer is right on the money. I'd just add this as a reference -
It doesn’t end there though you can use a case statement without giving it a value to match against, which allows a case statement to mimic the behavior of an if statement, e.g.:
print "Enter a string: "
some_string = gets.chomp
case
when some_string.match(/\d/)
puts 'String has numbers'
when some_string.match(/[a-zA-Z]/)
puts 'String has letters'
else
puts 'String has no numbers or letters'
end
I read over this guide quick, and it seems to be that you're trying to use the case as an if statement, but you supplied a value to match against.
Because you gave num as the first argument of case, it's expecting to match against it. The problem is, your conditions evaluate to boolean values, and num is a number, so they'll never match, and the else condition will always be run.
Remove the num from the start of the case.

How do I use elsif and ranges?

I have the following code:
var_comparison = 5
print "Please enter a number: "
my_num = Integer(gets.chomp)
if my_num > var_comparison
print "You picked a number greater than 5!"
elsif my_num < var_comparison
print "You picked a number less than 5!"
elsif my_num > 99
print "Your number is too large, man."
else
print "You picked the number 5!"
end
The interpreter has no way of distinguishing between accepting the rule >5 or >99. How do I make it so that any number between 6-99 returns "You picked a number greater than 5!", but a number 100 or greater returns "Your number is too large, man!"?
Do I need to specifically state a range somehow? How would I best do that? Would it by the normal range methods e.g.
if my_num 6..99
or
if my_num.between(6..99)
?
You can express it as a range, but it would be much simpler to rearrange the order of your conditions to achieve what you want. The interpreter runs through conditional if/else statements in the order they are written, stopping when a condition is true or when else is reached. This makes the order important. We can know that if I get to an elsif, all the preceding conditions must have been false. So in your code:
var_comparison = 5
print "Please enter a number: "
my_num = Integer(gets.chomp)
if my_num > 99
# my_num is > 99
print "Your number is too large, man."
elsif my_num > var_comparison # to get here, my_num must be <= 99
print "You picked a number greater than 5!"
elsif my_num < var_comparison
print "You picked a number less than 5!"
else
print "You picked the number 5!"
end
If you needed to express a number as a range (if your conditional logic becomes more complex), you could do the following:
if (6..99).cover?(my_num)
Clearly the interpreter has no way of distinguishing between accepting the rule >5 or >99.
Yes it does: it tests the conditions in textual order! Since 100 is both greater than 5 and greater than 99, both conditions match, but in an if/elseif chain only one condition is ever evaluated. You should move the clauses around to achieve what you want.
Rather than use elseif, use case/when:
def test_range(foo)
case foo
when 5 .. 99
puts "#{ foo } is in range"
else
puts "#{ foo } is out of range"
end
end
test_range(4)
test_range(5)
test_range(99)
test_range(100)
# >> 4 is out of range
# >> 5 is in range
# >> 99 is in range
# >> 100 is out of range
In your code's case, it needs to be a bit different, because testing for less-than and greater-than doesn't lend itself to the normal range tests, because they expect discrete minimum and maximum values. So, instead, this is what I'd do:
my_num = 5
case
when my_num > 99
puts "Your number is too large, man."
when my_num > 5
puts "You picked a number greater than 5!"
when my_num < 5
puts "You picked a number less than 5!"
else
puts "You picked the number 5!"
end
I find that long chains of if/elseif/else become harder to read, so I prefer case statements.
As Kilian said, if clauses are evaluated in order, so the first to match is considered the chosen one.
You can even use case; when to achieve this:
Infinity = 1.0/0
var_comparison = 5
print "Please enter a number: "
my_num = Integer(gets.chomp)
case my_num
when -Infinity...var_comparison
puts "You picked a number less than #{var_comparison}!"
when var_comparison
puts "You picked the number #{var_comparison}!"
when var_comparison..99
puts "You picked a number greater than #{var_comparison}!"
else
puts "Your number is too large, man."
end

ruby won't print output

I'm trying to see if the user enters a prime number. I want it to print "PRIME" or "NOT PRIME" to screen:
prime = ""
puts "TYPE IN A NUMBER TO SEE IF IT'S PRIME: "
gets.chomp(prime).to_i
for divide_by in 2..(prime.to_i - 1)
if prime % divide_by == 0
puts "NOT PRIME!!!"
else
puts "PRIME!!!"
end
end
Should I use a while loop instead?
Your gets.chomp(prime).to_i is not doing anything significant. I don't understand the purpose of chomp here, and it is not assigning any variable. If you want to receive the input number as prime, you need to do prime = gets.to_i.
Once you do that, you do not need to do to_i again as in for divide_by in 2..(prime.to_i - 1).
Furthermore, your logic is flawed. If prime is not divisible by 2, is that enough to say "PRIME!!"? I don't think so.
And if you are wondering what type of loop to use, the for loop is rarely useful. You should use each.

Resources