Forum: Ruby Dynamic Constant Assignment

076a2b44d85eb8731301da032cc4280c?d=identicon&s=25 GLENN CHAMBERLAIN (rubynewb)
on 2017-02-28 03:50
Good evening everyone,
I have posted my code below but am unable to determine what is causing a
persistent "Dynamic constant assignment" error at

"Console_Screen = Screen.new" and "SQ = Quiz.new"

on line 144 and 146. Any help or insight at all would be great. I've had
this issue before and it was fixed with "end". I went through and to me
it seems like all of the end statements are in the appropriate place.


class Screen

  def cls
    puts ("\n" * 25)
    puts "\a"

   end

 def pause
               #Pauses the display until the player presses the enter
key
     STDIN.gets

 end

 class Quiz


  def display_greeting
    Console_Screen.cls #Clears display
    print "\t\t Welcome to the SUperman Movie Trivia Quiz!" +
      "\n\n\n\n\n\n\n\n\n\nPress Enter to" +
      "continue."

      Console_Screen.pause  #Pauses the game
  end
  def display_instructions #Method to present quiz instructions

    Console_Screen.cls

    puts "INSTRUCTIONS:\n\n" #Displays a heading
    puts "You will be presented with a series of" +
      "multiple-choice"
    puts "questions. To answer a question, type in" +
      "the letter of"
    puts " the corresponding answer and press the" +
      "Enter Key. Your"
    puts "grade will be displayed at the end of the" +
      "test\n\n\n"
    puts "Good luck!\n\n\n\n\n\n\n"
    print "Press Enter to continue."

    Console_Screen.pause    #Pause the game
  end

    def disp_q(question, q_A, q_B, q_C, q_D, answer)

      #loop until a valid answer is input
      loop do

        Console_Screen.cls
        #formats the display of the questions

        puts question + "\n\n"
        puts q_A
        puts q_B
        puts q_C
        puts q_D

        print "\nType the letter representing your answer:"

        reply = STDIN.gets #collect the answer
        reply.chop!

        #Analyze the input
        if answer == reply then
          $noRight += 1
        end


        if reply == "a" or reply == "b" or reply =="c" or
          reply == "d" then

          break
        end
     end



      #Method for grading the quiz
        def determine_grade


          Console_Screen.cls

          #To pass they must answer 3 questions correctly
          if $noRight >= 3 then
            print "You correctly answered " + $noRight.to_s + "
            question(s)."
            puts "You have passed the \nSuperman Movie Trivia" +
              "Quiz!\n\n"
            puts "You have earned a rank of: Good Citizen" if
            $noRight == 3
            puts "You have earned a rank of: Side Kick"
            if $noRight == 4
            puts "You have earned the rank of: SuperHero"
            if $noRight == 5
              print "\n\nPress Enter to continue."

            else #they failed

              print "You missed" + (5 - $noRight).to_s +
                "questions."
              puts "You have failed the Superman Movie Trivia Quiz."
              puts "Perhaps you should watch the movies again before
              returning to"
              puts "retake the quiz"
              print "\n\nPress Enter to continue"

            end



            Console_Screen.pause



            def display_credits

              Console_Screen.cls

              puts"\t\tThank you for taking the SUPERMAN MOVIE TRIVIA
              QUIZ!\n\n\n\n"
              puts "\n\t\t\t Developed by Glenn B Chamberlain\n\n"
              puts "\t\t\t\t Copyright 2017\n\n"
              puts "\t\t\tURL:
http://www.glennisthebestprogrammerever.com\n\n\n\n"


          end




  #Main Script
Logic----------------------------------------------------------------

            #Keep track of the number of correct answers
              $noRight = 0
#Where the problem is shown when I try to run the code-------------
              Console_Screen = Screen.new

              SQ = Quiz.new
#----------------------------------------------------------------
              #Execute the display_greeting method

              SQ.display_greeting

              answer = ""
              #loop until the play enters y or n only

              loop do

                Console_Screen.cls
                print "Are you ready to take the quiz? (y/n): "

                answer = STDIN.gets
                answer.chop!

                break if answer == "y" || answer == "n"



              #analyze the players input
                if answer == "n"

                  Console_Screen.cls

                  puts "Okay, perhaps another time.\n\n"

                else

                  SQ.display_instructions #executes the
display_instructions method

                  #execute the disp_q method and pass it arguments
(questions, answers and correct answers)

              SQ.disp_q("What is the name of the Daily Planet's ace" +
                "photographer?", "a. Jimmy Olsen", "b. Clark Kent", +
                    "c. Lex Luthor", "d. Lois Lane", "a")

       #Call upon the disp_q method and pass it the second questions

         SQ.disp_q("What is the name of Clark Kents home town?",
                     "a. Metropolis", "b. Gotham City", "c.Smallville",
                      "d. New York", "c")
                    SQ.disp_q("In which movie did Superman battle" +
                      "General Zod?", "a. Super", "b. Superman II",
                      "c. Superman III", "d. Superman IV", "b")
                    SQ.disp_q("What is the name of Superman's father?",
                   "a. Nimo","b. Jarrell", "c. Lex Luthor",
"d.Krypton","b")
                    SQ.disp_q("Where had Superman been at the start of "
+         "Superman Returns?", "a. Moon", "b. Fortress of Solitude",
                      "c. Earth's Core", "d. Krypton", "d")

          #call up the determine_grade method to display the grade

                      SQ.determine_grade

                      SQ.display_credits
                end
076a2b44d85eb8731301da032cc4280c?d=identicon&s=25 GLENN CHAMBERLAIN (rubynewb)
on 2017-03-01 02:05
I was able to figure it out. And no my "end"s were not correct lol. I
need to pay better attention to their locations. This is the script that
works:

# Define Custom
Class--------------------------------------------------------
class Screen

  def cls
    puts ("\n" * 25)
    puts "\a"

   end

 def pause
               #Pauses the display until the player presses the enter
key
     STDIN.gets

 end
end

 class Quiz


  def display_greeting
    Console_Screen.cls #Clears display
    print "\t\t Welcome to the SUperman Movie Trivia Quiz!" +
      "\n\n\n\n\n\n\n\n\n\nPress Enter to" +
      "continue."


      Console_Screen.pause  #Pauses the game
  end
  def display_instructions #Method to present quiz instructions

    Console_Screen.cls

    puts "INSTRUCTIONS:\n\n" #Displays a heading
    puts "You will be presented with a series of" +
      "multiple-choice"
    puts "questions. To answer a question, type in" +
      "the letter of"
    puts " the corresponding answer and press the" +
      "Enter Key. Your"
    puts "grade will be displayed at the end of the" +
      "test\n\n\n"
    puts "Good luck!\n\n\n\n\n\n\n"
    print "Press Enter to continue."

    Console_Screen.pause    #Pause the game
  end

    def disp_q(question, q_A, q_B, q_C, q_D, answer)

      #loop until a valid answer is input
      loop do

        Console_Screen.cls
        #formats the display of the questions

        puts question + "\n\n"
        puts q_A
        puts q_B
        puts q_C
        puts q_D

        print "\nType the letter representing your answer:"

        reply = STDIN.gets #collect the answer
        reply.chop!

        #Analyze the input
        if answer == reply then
          $noRight += 1
        end


        if reply == "a" or reply == "b" or reply =="c" or
          reply == "d" then

          break

        end
     end
  end



      #Method for grading the quiz
        def determine_grade


          Console_Screen.cls

          #To pass they must answer 3 questions correctly
          if $noRight >= 3 then
            print "You correctly answered " + $noRight.to_s + "
            question(s)."
            puts "You have passed the \nSuperman Movie Trivia" +
              "Quiz!\n\n"
            puts "You have earned a rank of: Good Citizen" if  $noRight
== 3
            puts "You have earned a rank of: Side Kick"    if $noRight
== 4
            puts "You have earned the rank of: SuperHero"  if $noRight
== 5
              print "\n\nPress Enter to continue."

            else #they failed

              print "You missed" + (5 - $noRight).to_s +
                "questions."
              puts "You have failed the Superman Movie Trivia Quiz."
              puts "Perhaps you should watch the movies again before
              returning to"
              puts "retake the quiz"
              print "\n\nPress Enter to continue"

            end


            Console_Screen.pause

     end


            def display_credits

              Console_Screen.cls

              puts"\t\tThank you for taking the SUPERMAN MOVIE TRIVIA
              QUIZ!\n\n\n\n"
              puts "\n\t\t\t Developed by Glenn B Chamberlain\n\n"
              puts "\t\t\t\t Copyright 2017\n\n"
              puts "\t\t\tURL:
http://www.glennisthebestprogrammerever.com\n\n\n\n"


          end





  #Main Script
Logic----------------------------------------------------------------

            #Keep track of the number of correct answers
              $noRight = 0

             Console_Screen = Screen.new

             SQ = Quiz.new

              #Execute the display_greeting method

              SQ.display_greeting

              answer = ""
              #loop until the play enters y or n only

              loop do

                Console_Screen.cls
                print "Are you ready to take the quiz? (y/n): "

                answer = STDIN.gets
                answer.chop!

                break if answer == "y" || answer == "n"
              end



              #analyze the players input
                if answer == "n"

                  Console_Screen.cls

                  puts "Okay, perhaps another time.\n\n"

                else

                  SQ.display_instructions #executes the
display_instructions method

                  #execute the disp_q method and pass it arguments
(questions, answers and correct answers)

                  SQ.disp_q("What is the name of the Daily Planet's ace"
+
                    "photographer?", "a. Jimmy Olsen", "b. Clark Kent",
+
                    "c. Lex Luthor", "d. Lois Lane", "a")

                    #Call upon the disp_q method and pass it the second
questions

                    SQ.disp_q("What is the name of Clark Kents home
town?",
                      "a. Metropolis", "b. Gotham City", "c.Smallville",
                      "d. New York", "c")
                    SQ.disp_q("In which movie did Superman battle" +
                      "General Zod?", "a. Super", "b. Superman II",
                      "c. Superman III", "d. Superman IV", "b")
                    SQ.disp_q("What is the name of Superman's father?",
                        "a. Nimo","b. Jarrell", "c. Lex Luthor", "d.
Krypton","b")
                    SQ.disp_q("Where had Superman been at the start of "
+
                      "Superman Returns?", "a. Moon", "b. Fortress of
Solitude",
                      "c. Earth's Core", "d. Krypton", "d")

                      #call up the determine_grade method to display the
grade

                      SQ.determine_grade

                      SQ.display_credits
                end

 end
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2017-03-03 23:26
Props for the effort - that actually was a lot of code.

I you are tired of the various puts, you can always do:

  alias e puts

Or something like that. I like it because it is so
short. Ruby has Kernel#p too I think but it is a bit
different, when you do "p object", I think it calls
the #inspect method. But for debugging it can be
nice, and pp is even better (though it requires a
require 'pp' line before you can use pp, unortunately)
076a2b44d85eb8731301da032cc4280c?d=identicon&s=25 GLENN CHAMBERLAIN (rubynewb)
on 2017-03-04 03:37
Thanks Robert.

Another thing I need to do with this code is make it to where the player
is given an option to press a key for a more detailed set of
instructions from within the "instructions" screen. My question is how
to make the script pull the new method when the user presses the key.


 def display_instructions #Method to present quiz instructions

    Console_Screen.cls

    puts "INSTRUCTIONS:\n\n" #Displays a heading
    puts "You will be presented with a series of" +
      "multiple-choice"
    puts "questions. To answer a question, type in" +
      "the letter of"
    puts " the corresponding answer and press the" +
      "Enter Key. Your"
    puts "grade will be displayed at the end of the" +
      "test\n\n\n"
    puts "Good luck!\n\n\n\n\n\n\n"
    print "Press Enter to continue or press (I) for grading
instructions"




  end

  def disp_scale
   puts "this is a test"
   Console_Screen.pause

  end
076a2b44d85eb8731301da032cc4280c?d=identicon&s=25 GLENN CHAMBERLAIN (rubynewb)
on 2017-03-04 04:50
Here's my final script! I got it all to work. Let me know what you
think! I know some of the words are pushed together and there may be
some spelling but I was aiming for function.

#---------------------------------------------------------------------------------
# Script Name: SupermanQuiz.rb
# Version:     1.0
# Author:      Glenn B. Chamberlain
# Date:        March 6, 2017
#-----------------------------------------------------------------------------------
# Define Custom
Class--------------------------------------------------------
class Screen

  def cls
    puts ("\n" * 25)
    puts "\a"

   end

 def pause
               #Pauses the display until the player presses the enter
key
     STDIN.gets

 end
 def instructionpause
   gets

 end
end

 class Quiz


  def display_greeting
    Console_Screen.cls #Clears display
    print "\t\t Welcome to the SUperman Movie Trivia Quiz!" +
      "\n\n\n\n\n\n\n\n\n\nPress Enter to" +
      "continue."


      Console_Screen.pause  #Pauses the game
  end
  def display_instructions #Method to present quiz instructions

    Console_Screen.cls

    puts "INSTRUCTIONS:\n\n" #Displays a heading
    puts "You will be presented with a series of" +
      "multiple-choice"
    puts "questions. To answer a question, type in" +
      "the letter of"
    puts " the corresponding answer and press the" +
      "Enter Key. Your"
    puts "grade will be displayed at the end of the" +
      "test\n\n\n"
    puts "Good luck!\n\n\n\n\n\n\n"
    print "Press Enter to continue or press (i) for grading
instructions"

    Console_Screen.instructionpause
    answer = gets
    answer.chop!

    if answer == "i"

      puts disp_scale
    else

    end


  end

  #Created for step 5
  def disp_scale
    Console_Screen.cls

   puts "You will be graded based on the number of questions you answer
correctly\n" +
     "You will be a SUPER HERO for 8 or more.\n" +
     "A SIDE KICK for 7.\n" +
     "And a GOOD CITIZEN for 6.\n\n\n" +
     "You will fail if you answer fewer than 6.\n\n\n" +
     "Press Enter to proceed to the quiz\n\n\n\n"
   Console_Screen.pause

  end
    def disp_q(question, q_A, q_B, q_C, q_D, answer)

      #loop until a valid answer is input
      loop do

        Console_Screen.cls
        #formats the display of the questions

        puts question + "\n\n"
        puts q_A
        puts q_B
        puts q_C
        puts q_D

        print "\nType the letter representing your answer:"

        reply = STDIN.gets #collect the answer
        reply.chop!

        #Analyze the input
        if answer == reply then
          $noRight += 1
        end


        if reply == "a" or reply == "b" or reply =="c" or
          reply == "d" then

          break

        end
     end
  end



      #Method for grading the quiz
        def determine_grade


          Console_Screen.cls

          #To pass they must answer 6 questions correctly per section 1
          if $noRight >= 6 then
            print "You correctly answered " + $noRight.to_s + "
            question(s)."
            puts "You have passed the \nSuperman Movie Trivia" +
              "Quiz!\n\n"
            puts "You have earned a rank of: Good Citizen" if  $noRight
== 6
            puts "You have earned a rank of: Side Kick"    if $noRight
== 7
            puts "You have earned the rank of: Super Hero"  if $noRight
>= 8
              print "\n\nPress Enter to continue."

            else #they failed

              print "You missed" + (5 - $noRight).to_s +
                "questions."
              puts "You have failed the Superman Movie Trivia Quiz."
              puts "Perhaps you should watch the movies again before
              returning to retake the quiz"
              #Section 4
              puts " Here is a link to brush up on some facts"
              puts " http://en.wikipedia.org/wiki/Superman_movies"
              print "\n\nPress Enter to continue"

            end


            Console_Screen.pause

     end


            def display_credits

              Console_Screen.cls

              puts"\t\tThank you for taking the SUPERMAN MOVIE TRIVIA
QUIZ!\n\n\n\n"
              puts "\n\t\t\t Developed by Glenn B Chamberlain\n\n"
              puts "\t\t\t\t Copyright 2017\n\n"
              puts "\t\t\tURL:
http://www.glennisthebestprogrammerever.com\n\n\n\n"

          Console_Screen.pause

          end





  #Main Script
Logic----------------------------------------------------------------

            #Keep track of the number of correct answers
              $noRight = 0

             Console_Screen = Screen.new

             SQ = Quiz.new

              #Execute the display_greeting method

              SQ.display_greeting

              answer = ""
              #loop until the play enters y or n only

              loop do

                Console_Screen.cls
                print "Are you ready to take the quiz? (y/n): "

                answer = STDIN.gets
                answer.chop!

                break if answer == "y" || answer == "n"
              end



              #analyze the players input
                if answer == "n"

                  Console_Screen.cls
                 #Added for step 3 to present the credits if the player
decides not to play
                  SQ.display_credits

                  Console_Screen.pause

                else


                  SQ.display_instructions  #executes the
display_instructions method

                  #execute the disp_q method and pass it arguments
(questions, answers and correct answers)

                  SQ.disp_q("What is the name of the Daily Planet's ace"
+
                    "photographer?", "a. Jimmy Olsen", "b. Clark Kent",
+
                    "c. Lex Luthor", "d. Lois Lane", "a")

                    SQ.disp_q("What is the name of Clark Kents home
town?",
                      "a. Metropolis", "b. Gotham City", "c.Smallville",
                      "d. New York", "c")
                    SQ.disp_q("In which movie did Superman battle" +
                      "General Zod?", "a. Super", "b. Superman II",
                      "c. Superman III", "d. Superman IV", "b")
                    SQ.disp_q("What is the name of Superman's father?",
                        "a. Nimo","b. Jarrell", "c. Lex Luthor", "d.
Krypton","b")
                    SQ.disp_q("Where had Superman been at the start of "
+
                      "Superman Returns?", "a. Moon", "b. Fortress of
Solitude",
                      "c. Earth's Core", "d. Krypton", "d")
                  SQ.disp_q("What is the name of the Daily Planet's ace"
+
                                    "photographer?", "a. Jimmy Olsen",
"b. Clark Kent", +
                                    "c. Lex Luthor", "d. Lois Lane",
"a")

                        #Addition questions for section 1

                               SQ.disp_q("What can Superman travel
faster than?",
                                      "a. Speeding Bullet", "b. Crawling
Baby", "c. Springing Springs",
                                      "d. Jumping Frogs", "a")
                                    SQ.disp_q("What can Superman shoot
from his eyes?",
                                      "a. Skittles", "b. Rainbows",
                                      "c. Lasers", "d. Other Superman",
"c")
                                    SQ.disp_q(" What is Supermans home
planet?",
                                        "a. Saturn","b. Mars", "c.
Avatar", "d. Krypton","d")
                                    SQ.disp_q("Who is Superman in love
with?", "a. Mary Jane", "b. Himself",
                                      "c. Barbara", "d. Louis", "d")
                                    SQ.disp_q("Who created the Superman
story?", "a. Jerry Siegel", "b. Himself",
                                                        "c. Joe
Shuster", "d. Both A and C", "d")

                      #call up the determine_grade method to display the
grade

                      SQ.determine_grade

           #Causes the player to be prompted to do the quiz again until
they can get over 6 correct answers for
           #part 2
       until $noRight >= 6
         print "Would you like to try again?(y/n)"

         answer = STDIN.gets
         answer.chop!
         if answer == "y"
           SQ.display_instructions  #executes the display_instructions
method

                             #execute the disp_q method and pass it
arguments (questions, answers and correct answers)

                             SQ.disp_q("What is the name of the Daily
Planet's ace" +
                               "photographer?", "a. Jimmy Olsen", "b.
Clark Kent", +
                               "c. Lex Luthor", "d. Lois Lane", "a")

                               SQ.disp_q("What is the name of Clark
Kents home town?",
                                 "a. Metropolis", "b. Gotham City",
"c.Smallville",
                                 "d. New York", "c")
                               SQ.disp_q("In which movie did Superman
battle" +
                                 "General Zod?", "a. Super", "b.
Superman II",
                                 "c. Superman III", "d. Superman IV",
"b")
                               SQ.disp_q("What is the name of Superman's
father?",
                                   "a. Nimo","b. Jarrell", "c. Lex
Luthor", "d. Krypton","b")
                               SQ.disp_q("Where had Superman been at the
start of " +
                                 "Superman Returns?", "a. Moon", "b.
Fortress of Solitude",
                                 "c. Earth's Core", "d. Krypton", "d")
                             SQ.disp_q("What is the name of the Daily
Planet's ace" +
                                               "photographer?", "a.
Jimmy Olsen", "b. Clark Kent", +
                                               "c. Lex Luthor", "d. Lois
Lane", "a")

                                   #Addition questions for section 1

                                          SQ.disp_q("What can Superman
travel faster than?",
                                                 "a. Speeding Bullet",
"b. Crawling Baby", "c. Springing Springs",
                                                 "d. Jumping Frogs",
"a")
                                               SQ.disp_q("What can
Superman shoot from his eyes?",
                                                 "a. Skittles", "b.
Rainbows",
                                                 "c. Lasers", "d. Other
Superman", "c")
                                               SQ.disp_q(" What is
Supermans home planet?",
                                                   "a. Saturn","b.
Mars", "c. Avatar", "d. Krypton","d")
                                               SQ.disp_q("Who is
Superman in love with?", "a. Mary Jane", "b. Himself",
                                                 "c. Barbara", "d.
Louis", "d")
                                               SQ.disp_q("Who created
the Superman story?", "a. Jerry Siegel", "b. Himself",
                                                                   "c.
Joe Shuster", "d. Both A and C", "d")

                                 #call up the determine_grade method to
display the grade

                                 SQ.determine_grade


         else



           SQ.display_credits
           Console_Screen.pause

         end
       end
    end
 end
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.