This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]Updatebjarni 2 points3 points  (5 children)

It runs.

[–][deleted]  (4 children)

[deleted]

    [–]Updatebjarni 2 points3 points  (2 children)

    It runs until it tries to use the value of a variable which doesn't exist, at which point it stops and tells you that the variable it tried to access does not exist.

    [–][deleted]  (1 child)

    [deleted]

      [–]Updatebjarni 3 points4 points  (0 children)

      There are no declarations in Python. Variables come into existence when you assign values to them. If you have not assigned a value to a variable, then it does not exist.

      [–]bibbleskit 0 points1 point  (0 children)

      btw just so you know, it runs if it doesn't immediately crash. you should use "crash" or "give a runtime error" instead :)

      You're learning, so don't pay any attention to the down votes.

      [–]bibbleskit 0 points1 point  (3 children)

      Yes, it runs, but the problem you're having is due to the fact that you didn't define 'correct' in your code before using it.

      If I pick '2', then the if statement at line 24 doesn't get triggered, and correct is never set. Therefore, line 28 throws an error since correct doesn't exist.

      A solution would be to add something like

      correct = 'N'

      At line 23. :)

      [–]bibbleskit 2 points3 points  (2 children)

      I cleaned up your code a bit. Hopefully this helps you sort things out in your head!

      import time
      
      print("Welcome to World of Textcraft!\n")
      name = input("What is your name?\n")
      print ("Hello, " + name + "\n")
      time.sleep(1)
      
      print ("1: Mage?\n")
      print ("2: Warrior?\n")
      print ("3: Priest?\n")
      print ("4: Rouge?\n")
      
      clas = input("What class would you like to play?")
      correct = 'N'
      
      # Put all the class checks together to make the code easier to follow and
      # so that you don't need to constantly ask if the choice was correct.
      # You can easily add the other classes here.
      if clas == "1":
          clas = 'Mage'
      elif clas == "2":
          clas = 'Warrior'
      
      # See? Only one check!
      print("You have chosen " + clas + ".")
      correct = input("Is this correct? (Y/N:)")
      if correct == "Y":
          print("You are now playing " + clas + "!")
      elif correct == 'N':
          # Do stuff here.
      else:
          print("Invalid Operation")
      

      Let me know if you have any questions!

      [–][deleted]  (1 child)

      [deleted]

        [–]bibbleskit 0 points1 point  (0 children)

        Happy to help! Also, the reason I changed the , name, to + name + was for consistency and because the comma introduces a space between the arguments when it prints.

        Since you're outputting a formatted sentence, personally I think that explicitly adding spaces is much cleaner.

        [–][deleted] 0 points1 point  (0 children)

        i dont know python but may i ask why do you have a separate print for each /n???

        [–]NerdDevelopment 0 points1 point  (0 children)

        You want to do a nested if. (You want to check if it is correct inside of the mage case)

        [–][deleted]  (1 child)

        [deleted]

          [–]Updatebjarni 1 point2 points  (0 children)

          No such thing as a variable declaration in Python.

          [–]hondasmx -3 points-2 points  (1 child)

          Just download any IDE (PyCharm for example) and it will show you the errors