Why Doesn't This Code Work, Python 3.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jharrison
    New Member
    • Jan 2010
    • 2

    Why Doesn't This Code Work, Python 3.0

    I'm finding it hard to make this code work in Python 3.0. Been looking at it for some time now:

    Code:
    # import module for random functions
    import random
    
    # List of words for the computer to pick from
    words = ("basketball", "football", "hockey", "rugby", "baseball")
    
    # Word to be guessed; picked at random 
    word = random.choice(words)
    letters_guessed = []
    
    print ("Guess the sport!")
    print ("You get to give five letters.")
    print ("There are %s letters in the word.") % (len(word))
    guesses = 5
    while guesses != 0:
    letter = input("Enter a letter: ")
    if letter in letters_guessed:
    print ("You already guessed that letter.")
    else:
    guesses = guesses - 1
    print ("You have %d guesses left.") % (guesses)
    letters_guessed.append(letter)
    
    
    print ("The word:")
    
    masked_word = ""
    for letter in word:
    if letter in letters_guessed:
    masked_word += letter
    else: masked_word += "-"
    print masked_word
    guess = input("Guess the word: ")
    if guess == word:
    print ("Congratulations, %s is the word!") % (guess)
    else:
    print ("Nope. The word is %s.") % (word)
    input ("Press The Enter Key To Exit")
    Last edited by bvdet; Jan 4 '10, 07:09 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags when posting code. See posting guidelines here.

    BV - Moderator

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      jharrison,

      You have no indentation in the code you posted. It could be as simple as that. What error message do you get when you run the code?

      Comment

      • jharrison
        New Member
        • Jan 2010
        • 2

        #4
        okay i have the issue solved, when it runs now I get this error message.


        Guess the sport!
        You get to give five letters.
        There are %s letters in the word.
        Traceback (most recent call last):
        File "C:\Users\Jamo\ Guess The Word.py", line 13, in <module>
        print ("There are %s letters in the word.") % (len(word))
        TypeError: unsupported operand type(s) for %: 'NoneType' and 'int

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          I was hoping that you would repost your code using code tags. There is no indentation in the code you posted. That makes it difficult to follow.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            I converted to code to run in Python 2.5 and guessed at the indentation:
            Code:
            # import module for random functions
            import random
             
            # List of words for the computer to pick from
            words = ("basketball", "football", "hockey", "rugby", "baseball")
             
            # Word to be guessed; picked at random 
            word = random.choice(words)
            letters_guessed = []
             
            print "Guess the sport!"
            print "You get to give five letters."
            print "There are %s letters in the word." % (len(word))
            guesses = 5
            while guesses != 0:
                letter = raw_input("Enter a letter: ")
                if letter in letters_guessed:
                    print "You already guessed that letter."
                else:
                    guesses = guesses - 1
                    print ("You have %d guesses left.") % (guesses)
                    letters_guessed.append(letter)
             
                print "The word:"
                masked_word = ""
                for letter in word:
                    if letter in letters_guessed:
                        masked_word += letter
                    else: masked_word += "-"
                print masked_word
                
            guess = raw_input("Guess the word: ")
            if guess == word:
                print "Congratulations, %s is the word!" % (guess)
            else:
                print "Nope. The word is %s." % (word)
            raw_input ("Press The Enter Key To Exit")
            It seems to work.

            Comment

            • sockmonk
              New Member
              • Jan 2010
              • 1

              #7
              I think the problem is the parentheses in your print statement. Try changing this:

              Code:
              print ("There are %s letters in the word.") % (len(word))
              To this:
              Code:
              print ("There are %s letters in the word." % len(word))
              That way the print function gets just one argument, an expression that evaluates to the finished string.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Thanks for the input sockmonk. Invalid parentheses around arguments to the print function would be consistent with the error message. I should have noticed it.

                Comment

                Working...