I'm trying to modify a script to include a case block but whenever I do I seem to get this error. I initially thought it was because I missed an end somewhere but I checked my entire code and it seems to check out fine.
Working (before adding case):
def determine_grade
Console_Screen.cls #Clear the display area
#To pass the test the player must correctly retype 3 sentences
if $noRight >= 6 then
#Inform the player of the good news
print "You retyped " + $noRight.to_s + " sentence(s)" +
"correctly. "
puts "You have passed the typing test!\n\nPress Enter" +
"to continue."
else #The player has failed the test
#Inform the player of the bad news
print "You retyped " + $noRight.to_s + " sentence(s)" +
"correctly. "
puts "You have failed the typing test!\n\nPress Enter
to continue."
end
end
end
After:
def determine_grade
Console_Screen.cls #Clear the display area
#To pass the test the player must correctly retype 6 sentences
case $noRight
when 9 || 10
print "You get an A!"
end
when 8
print "You get a B!"
end
when 7
print "You get a C."
end
when 6
print "You get a D."
end
when <= 5
print "You get an F."
end
else
print "Error"
end
end
end
end
Any ideas?
You don't need the end statements inside of the when block. That's what's throwing the interpreter off. Also, your multi-value when is wrong; it should be when 9, 10 instead of when 9 || 10 since that will evaluate to a truthy value.
Remove those and your new code should be equivalent to your original code.
def determine_grade
Console_Screen.cls #Clear the display area
#To pass the test the player must correctly retype 6 sentences
print case $noRight
when 9..10 then "You get an A!"
when 8 then "You get a B!"
when 7 then "You get a C."
when 6 then "You get a D."
when -Float::INFINITY..5 then "You get an F."
else "Error"
end
end
Related
I am very new to TI-BASIC, and I've been trying to make a guessing game. The code outputs no errors, but displays the wrong version of "higher" or "lower" when you put in a number
Here is the code:
ClrHome
randInt(1,100)→N
For(B,1,10)
Input "Guess a number: ",A
If A=N:Then
Disp "You did it"
Stop
Else
If A>N:Then
Disp "You are higher"
Else
Disp "You are lower"
End
End
End
Disp "You lose"
Disp "The number was",N
Stop
This question already has answers here:
Dynamic constant assignment
(7 answers)
Closed 2 years ago.
I have been following a tutorial to create a typing challenge. I have taken care to follow this carefully. When i try to run the script from the command line i keep getting the following error and i do not understand it. I think the tutorial might be quite old but if someone could give me some guidance to understand it so i can fix it then that would be so appreciated! The error i get when i run the script from the command line is as follows....
Typechallenge.rb:89: dynamic constant assignment
Console_Screen = Screen.new
^
typechallenge.rb:90: dynamic constant assignment
Typing_Test = Test.new
The script itself is below...
#Script name: Typing Challenge
#Description: Demonstrating how to apply conditional logic in order to analyze user input and control
#the execution of the script through a computer typing test.
class Screen
def cls
puts ("\n" * 25)
puts "\a"
end
def pause
STDIN.gets
end
end
class Test
def display_greeting
Console_Screen.cls
print "\t\t Welcome to the Typing Challenge" +
"\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
"continue. \n\n: "
Console_Screen.pause
end
def display_instructions
Console_Screen.cls
puts "\t\t\tInstructions:\n\n"
puts %Q{ This test consists of five typing challenges. Each sentence is a challenge and are presented one at a time. To respond
correctly you should retype each sentence exactly as it is shown and the press the Enter key. Your grade will be displayed at
the end of the test.\n\n\n\n\n\n\n\n\n
Press Enter to continue.\n\n}
Console_Screen.pause
End
def present_test(challenge)
Console_Screen.cls
print challenge + "\n\n: "
result = STDIN.gets
result.chop!
if challenge == result then
$noRight += 1
Console_Screen.cls
print "Correct!\n\nPress Enter to continue."
Console_Screen.pause
else
Console_Screen.cls
print "Incorrect!\n\nPress Enter to continue."
Console_Screen.pause
end
end
def determine_grade
Console_Screen.cls
if $noRight >= 3 then
print "You retyped " + $noRight.to_s + " sentence(s) correctly. "
puts "You have passed the typing test!\n\nPress Enter to continue."
else
print "You retyped " + $noRight.to_s + " sentence(s) correctly. "
puts "You have failed the typing test!\n\nPress Enter to continue."
end
end
#Main script logic
$noRight = 0
Console_Screen = Screen.new
Typing_Test = Test.new
Typing_Test.display_greeting
Console_Screen.cls
print "Would you like to test your typing skills? (y/n)\n\n: "
answer = STDIN.gets
answer.chop!
until answer == "y" || answer == "n"
Console_Screen.cls
print "Would you like to test your typing skills? (y/n)\n\n: "
answer = STDIN.gets
answer.chop!
end
#Analyzing the players response
if answer == "n"
Console_Screen.cls
puts "Okay, perhaps another time! \n\n"
else
Typing_Test.display_instructions
Typing_Test.present_test "In the end there can be only one"
Typing_Test.present_test "Once upon a time a great plague swept across the land"
Typing_Test.present_test "Welcome to the typing challenge"
Typing_Test.present_test "There are very few problems in the world" + "that enough M&Ms cannot fix."
Typing_Test.present_test "Lets play this game of life together"
Typing_Test.determine_grade
Console_Screen.pause
Console_Screen.cls
puts "Thank you for playing the game!\n\n"
end
end
end
Names that start with upper-case letter are constants. In your code you assign a non-constant (dynamic) value to a name that represents a constant. Hence the error.
Console_Screen = Screen.new
Use local variable name convention (snake_case)
console_screen = Screen.new
I'm New to ruby,learning basics,I want to enter only Integer Values in age field,Need to Throw Not a number when it is a string when executing the following code
puts "Enter Age "
age=Integer(gets) rescue nil
if age.is_a?(Numeric)
puts "Your age is #{age}"
else
puts "Not a Number"
end
if age>25
puts "You are Permitted"
else
puts "Not allowed"
end
getting error as ': undefined method `>' for nil:NilClass (NoMethodError) What am doing wrong ?
The user input was not in a format to give an integer, so by rescue, age became nil. You tried to apply > on it in if age>25, which is not defined.
You wrote puts "Not a number" which will print a message but then your program will keep running as usual. On that line, try replacing "puts" with "raise" and then read about Ruby exceptions.
You don't need to rescue to nil because you're planning to get and respond to whatever the user inputs and you don't need to raise because you don't want the program to exit after the user inputs a non Integer.
This is probably what you need:
age = ""
loop do
puts "Enter Age "
age = gets.chomp
if age.to_i.to_s == age.to_s
puts "Your age is #{age}"
else
puts "Not a Number"
next
end
if age.to_i > 25
puts "You are Permitted"
break
else
puts "Not allowed"
break
end
end until age.to_i.to_s == age.to_s
next will make it go back and do the next loop, and break will break out of the loop.
You can use age.to_i.to_s == age.to_s to really check if age is an integer.
I'm having a problem with my ruby script. If anyone could help, I'd really appreciate it. The problem is that the number is stuck between 1-2; where 2 is too high and 1 is too low. The guesses should be integers only.
#!/usr/bin/ruby
def highLow(max)
again = "yes"
while again == "yes"
puts "Welcome to the High Low game"
playGame(max)
print "Would you like to play again? (yes/no): "
again = STDIN.gets.chomp
if again == 'no'
puts "Have a nice day, Goodbye"
end
end
end
#This method contains the logic for a single game and call the feedback method.
def playGame(max)
puts "The game gets played now"
puts "I am thinking of a number between 1 and #{max}." #It show what chosen by user
randomNumber = rand(max)+ 1
print "Make your guess: "
guess = STDIN.gets.chomp
feedback(guess, randomNumber)
end
#Start while loop
#Logic for feedback method. It's ganna check the guess if it's high or low.
def feedback(guess, randomNumber)
count = 1
while guess.to_i != randomNumber
count = count + 1
if guess.to_i < randomNumber
print "That's too low. Guess again: "
else
print "That's too high. Guess again: "
end
guess = STDIN.gets.chomp
end
puts "Correct! You guessed the answer in #{count} tries!"
end
highLow(ARGV[0])
Change your last line to this:
highLow(ARGV[0].to_i)
The ARGV array contains all the passed in arguments as strings, so you have to cast it to integer.
begin
selected_option = gets.chomp
if selected_option == 1
puts "Welcome to the Welcome Screen!"
elsif selected_option == 2
puts "This is the options menu."
elsif selected_option == 3
puts "Logging out. Goodbye!"
else
puts "Please select a valid option."
end
end while not selected_option == 3
I enter in 1 or 2 or 3 and I always get the "Please enter a valid option" message. I'm guessing that's because the chomp method retrieves the input as a string.
Any way around this sans using quotes around the options?
gets.chomp.to_i
Will convert it to an integer.
You may also want to use a switch instead:
begin
selected_option = gets.chomp.to_i
case selected_option
when 1
puts "Welcome to the Welcome Screen!"
when 2
puts "This is the options menu."
when 3
puts "Logging out. Goodbye!"
else
puts "Please select a valid option."
end
end while not selected_option == 3