2014-09-25 4 views
-1

저는 기본 프로그래밍 수업을하고 있는데, 저는이 게임을하기 위해 조금씩 노력하고 있습니다. 아이디어는 컴퓨터가 임의의 단어를 선택하고 단어의 첫 문자를 추측하려고 시도하는 간단한 단어 추측 게임을 작성한 다음 5 단어 시도 후에 단어 자체를 추측하려고했습니다. 나는 여러 번 지나갔고 모듈을 실행하려고하면 "잘못된 구문"오류가 발생합니다. 나는 프로그래밍 언어에 관해서 약간 난독증을 느낀다. 그래서 내가 바라보고있는 것이 있을까? 만약 누군가가 약간의 도움을 줄 수 있다면 나는 그것을 고맙게 여길 것이다!Word Guessing Game in Python?

#Word Guessing Game 
#Computer picks a random word 
#Player tries to guess it 
#computer only responds with yes or no 

import random 

tries = 0 

print "Welcome to the word game!" 
print "\nI'm going to think of a word and you have to guess it!" 
print "\nGuess which letters are in the word, then you have to guess the whole thing!" 
print "\nGood luck!" 

WORDS = ("follow", "waking", "insane", "chilly", "massive", 
     "ancient", "zebra", "logical", "never", "nice") 

word = random.choice(WORDS) 

correct = word 
length = len(word) 
length = str(length) 

guess = raw_input("The word is " + length + " letters long. Guess a letter!: ") 

while tries < 5: 
    for guess in word: 
     if guess not in word: 
      print "Sorry, try again." 
     else: 
      print "Good job! Guess another!" 

    tries = tries + 1 #* 

    if tries = 5: 
     final = raw_input ("Try to guess the word!: ") 

     if final = correct: 
      print "Amazing! My word was ", word, "!" 

     else: 
      print "Sorry. My word was ", word, ". Better luck next time!" 

raw_input("\n\nPress enter to exit") 

또한, I는 "시도"변수의 범위를 지정하려고 시도 할 때 문제가 "동안 시도"블록의 종료 후에 발생했음을 주목해야한다. 이전에 임의의 숫자로 게임을 해봤지만 어떤 이유로 여기에서 제대로 작동하지 않았습니다. 나는 몇몇 도움을 크게 평가할 것이다! 필자는 파이썬의 구식 버전 인 2.0 버전을 사용하고 있다고 생각합니다.

+0

'할 말이 맞다'는 것을 어떻게 기대합니까? – njzk2

+0

@ Hodge-PodgeCrush, 나는 OP에서 공백을 제거했다. '# * '가있는 행에 코드에서와 같은 질문에 같은 들여 쓰기가 있는지 확인하십시오. 그 밖의 모든 것은 괜찮아 보였다. – jedwards

답변

1
tries = tries + 1 

    if tries == 5: 
     final = raw_input ("Try to guess the word!: ") 

     if final == correct: 

당신은 =은 할당을위한 비교하지 =에 대한 ==이 필요합니다.

또한 추측이 모두 소진 될 때 당신은 또한 루프 내에서 raw_input를 이동하고 바로 동안 외부 단어를 추측하도록 사용자에게 물어보고 싶은 tries += 1

으로 tries = tries + 1을 대체 할 수

while tries < 5: 
    guess = raw_input("The word is " + length + " letters long. Guess a letter!: ") 
    if guess not in word: # use `in` to check if the guess/letter is in the word 
     print "Sorry, try again." 
    else: 
     print "Good job! Guess another!" 

    tries += 1 

# guesses are all used up when we get here 
final = raw_input ("Try to guess the word!: ") 

if final == correct: 
    print "Amazing! My word was ", word, "!" 

else: 
    print "Sorry. My word was ", word, ". Better luck next time!" 
0

이것을 시도하십시오.

import random 

index = -1 
infi = ("python","jumble","hide","mama") 

word = random.choice(infi) 
word_len = len(word) 
guess = "" 
attempt = 0 
enter = "" 
print(word) 
temp = "_" * word_len 

print("\t\t The word chosen by computer contain", word_len,"letters.") 
print("\t Tap any letter to check if this letter is in computer word.\n") 
print("\t You got 5 attempts to check if tapped letter is in computer word.\n") 
print("\t\t\t\t GOOD LUCK!!!\n\n\n\n") 

for i in range(0, 5): 
    attempt +=1 
    guess = input("Attempt no. "+str(attempt)+":") 
    if guess in word and guess != enter: 
     for i in range(0, word_len): 
      if guess == word[i]: 
       temp = temp[:i] + guess +temp[i+1:] 
     print("yes\n" + temp) 
    if guess not in word: 
     print("no") 
    if "_" not in temp: 
     print("\t\t*********** Congratulation!! You guess the word *************") 
     break 
    elif attempt == 5: 
     guess = input("And the word is:") 
     if guess == word: 

      print("\t\t*********** Congratulation!! You guess the word *************") 
     else: 
      print("\t\t*********** WRONG!! Shame on you *************")