Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

Hangman Game Python Error

I m trying to create the hangman game in python, but got following error when I tried to run the file.

guess = input(" Enter your guess: ")

File string", line 1, in module

Please let me know if you know the solution to fix this error

#!/usr/bin/python
# imports
import random

# constants
HANGMAN = (
"""
------
|    |
|
|
|
|
|
|
|
----------
""",
"""
------
|    |
|    O
|
|
|
|
|
|
----------
""",
"""
------
|    |
|    O
|   -+-
|
|  
|  
|  
|  
----------
""",
"""
------
|    |
|    O
|  /-+-
|  
|  
|  
|  
|  
----------
""",
"""
------
|    |
|    O
|  /-+-/
|  
|  
|  
|  
|  
----------
""",
"""
------
|    |
|    O
|  /-+-/
|    |
|  
|  
|  
|  
----------
""",
"""
------
|    |
|    O
|  /-+-/
|    |
|    |
|   |
|   |
|  
----------
""",
"""
------
|    |
|    O
|  /-+-/
|    |
|    |
|   | |
|   | |
|  
----------
""")

MAX_WRONG = len(HANGMAN) - 1
WORDS = ("OVERUSED", "CLAM", "GUAM", "TAFFETA", "PYTHON", "HARDWICKE", "BRADLEY", "SHEFFIELD")

# initialize variables
word = random.choice(WORDS)   # the word to be guessed
so_far = "-" * len(word)      # one dash for each letter in word to be guessed
wrong = 0                     # number of wrong guesses player has made
used = []                     # letters already guessed


print("Welcome to Hangman.  Good luck!")

while wrong < MAX_WRONG and so_far != word:
    print(HANGMAN[wrong])
    print("
You've used the following letters:
", used)
    print("
So far, the word is:
", so_far)

    print("
Only use one letter values, more than one letter at a time does not work at this time")
    guess = input("
Enter your guess: ")
    guess = guess.upper()

    while guess in used:
        print("You've already guessed the letter", guess)
        guess = input("Enter your guess: ")
        guess = guess.upper()

    used.append(guess)

    if guess in word:
        print("
Yes!", guess, "is in the word!")

        # create a new so_far to include guess
        new = ""
        for i in range(len(word)):
            if guess == word[i]:
                new += guess
            else:
                new += so_far[i]              
        so_far = new

    else:
        print("
Sorry,", guess, "isn't in the word.")
        wrong += 1

if wrong == MAX_WRONG:
    print(HANGMAN[wrong])
    print("
You've been hanged!")
else:
    print("
You guessed it!")

print("
The word was", word)

input("

Press the enter key to exit.")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  guess = raw_input("
Enter your guess: ")

It should be raw_input, not input.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...