2015-07-11 116 views
1

我是全新的Python,但有Matlab和C++背景。請幫忙!!!!Python:Hang子手遊戲問題

我有我的hang子手代碼有問題。如果一個單詞有多個相同的字母,我無法弄清楚如何讓它切換全部。我有幾個嘗試與他們中的一些註釋掉。

import random 
import time 
import sys 

def pickWord(): 
    words = [line.strip() for line in open('word_list.txt')] 
    word = random.choice(words) 
    word = word.lower() 
    return word 

def checkLetter(word, input): 
    if input not in word: 
     in_letter = 0 
    else: 
     in_letter = 1 
    return in_letter 

def check_input(input): 
    if input.isaplha() == False : 
     input = raw_input('Your input was not a letter, please enter a letter: ') 
    elif len(input) > 0: 
     input = raw_input('Your entry was longer than 1 letter, please enter  one letter: ') 
    else: 
     input = input 
    return input 



#main function 

running = 'y' 

print('Lets Play Hangman!\n\n ------------------------ \n\nGenerating a Random word \n\n') 

while running == 'y': 

word = pickWord() 
letters = list(word) 

time.sleep(3) 

print ('The word has been chosen\n\n') 

print '%s' % word 

start = raw_input('Are you ready to start?(y/n)') 
start = start.lower() 

if start == 'n': 
    print('Well, too damn bad. Here We go!\n\n **************************\n\n') 
elif start == 'y': 
    print('Awesome, lets begin!\n\n*********************************\n\n') 
else: 
    print('You did not enter y or n, sorry you are not allowed to play!') 
    sys.exit() 

i = 0 

print ('The word has %d letters in it') % len(word) 

input = raw_input('What is your first guess: ') 
input = input.lower() 
correct = ['_'] * len(word) 

print ' '.join(correct) 

while correct != letters and i <= 5: 
    '''if input in letters: 
     for input in letters: 
      location = letters.index(input) 
      correct[location] = input 
     print('You guessed the correct letter! Your input %s is in the word at the %d spot.') % (input, location) 
     print ' '.join(correct) 
    elif input not in letters: 
     print('You guessed wrong :(') 
     i = i + 1 
     guesses = 6 - i 
     print('You have %d guesses left.') % guesses 
     guesses = 0 
    else: 
     print('You did not enter a valid letter, please try again.')''' 

    '''for j in letters: 
     if j == input: 
      location = letters.index(j) 
      correct[location] = j 
      print '%s' % ' '.join(correct) 
      print '%d' % location 
      print '%s' % j 
     if j == input: 
      location = letters.index(j) 
      correct[location] = j 
     print('You guessed the correct letter! Your input %s is in the word at the %d spot.') % (input, location) 
     print ' '.join(correct)''' 

    if input not in letters: 
     i = i + 1 
     guesses = 6 - i 
     print("You guessed incorrectly. You have %d guesses left.") % guesses 


    input = raw_input('What is your next guess: ') 
    input = input.lower() 

if correct == letters: 
    print('Congrats! You won the game!') 
else: 
    print('You lost. :(') 

running = raw_input('Do you want to play again? (y/n)').lower() 
+1

嗨,歡迎來到StackOverflow。請編輯您的帖子,只包含儘可能少的代碼來複制您的問題,並且您會更快地得到答案。 – johnnyRose

回答

1

在您的嘗試中,循環在找到輸入的第一個匹配字符時停止。

下面的代碼將工作:

guess = raw_input('What is your first guess: ') 
word = "wordword" 
letters = list(word) 
correct = ['_']* len(word) 
for x, y in enumerate(word): 
    if guess == y: 
     correct[x] = y 

你的錯誤

在你第一次嘗試:

if input in letters: 
    for input in letters: 

要檢查,如果輸入的字母,這很好,但如果返回True,輸入原始值將丟失,並且在循環訪問letters的元素時重新分配。

>>>input = "w" 
>>>word = "word" 
>>>if input in word: 
... for input in word: 
...  print(input) 

w 
o 
r 
d 

你的第二次嘗試

for j in letters: 
    if j == input: 
     location = letters.index(j) 

接近很多是成功的,但是location = letters.index(j)總是要等於j的第一場比賽的索引,從而將不會分配所有匹配的輸入值。