2013-02-01 35 views
0

我正在Python中爲GCSE計算任務做一個Hangman遊戲,我所要做的就是確保當有人輸入正確的字母時,在這個詞的正確位置。 守則如下:Python:Hang子手遊戲,沒有在正確的地方顯示字母

def guess_part(word): 
    lives = 6 
    LetterCount = 0 
    LetterMask = "" 
    for x in range(len(word)): #run for loop for the amount of the length of word 
     LetterMask = LetterMask + "*" 
    print LetterMask 
    while lives != 0 and LetterMask.find("*")!=-1: #while lives are not 0 and the amount of asterisks are not -1 
     LetterGuess = raw_input("Enter a letter to guess?") 
     LetterCount = 0 
     for char in word: 
      LetterCount = LetterCount + 1 
     if LetterGuess not in word: 
      lives = lives - 1 
     else: 
      if LetterGuess in word and lives != 0: 
       print "Good Guess." 
       LetterMask = list(LetterMask) 
       LetterMask[LetterCount-1] = LetterGuess 
       LetterMask = "".join(LetterMask) 
       print LetterMask 
      else: 
       print "Incorrect." 
    if lives == 0: 
     print "You have ran out of lives, your word was: ", word 
    else: 
     print "You have correctly guess the word! Score: ", lives 
     print "Play again?" 
     again = raw_input("") 
     again.lower() 
     if again == "y": 
      menu() 
     elif again == "n": 
      exit() 
     else: 
      exit() 


def rand_word(): 
    from random import randrange 
    random_words = ['extraordinary','happy','computer','python','screen','cheese','cabaret','caravan','bee','wasp','insect','mitosis','electronegativity','jumper','trousers'] 
    word = random_words[randrange(0, 15)] #pick a random number, and use this number as an index for the list, "random_words". 
    guess_part(word) #call the function, "guess_part" with the parameter "word" 

def user_word(): 
    print "All words will be changed to lowercase." 
    print "Enter the word you would like to guess." 
    print "" 
    validation_input = False 
    while validation_input == False: #while the validation input is not False, do below. 
     word = raw_input("") 
     if word.isalpha(): #If word contains only strings, no numbers or symbols, do below. 
      word = word.lower() #set the string of variable, "word", to all lowercase letters. 
      guess_part(word) #call the function, "guess_part" with the parameter, "word". 
      validation_input = True #Break the while loop 
     else: 
      print "Word either contained numbers or symbols." 

def menu(): 
    print "Hangman Game" 
    print "" 
    print "Ashley Collinge" 
    print "" 
    print "You will have 6 lives. Everytime you incorrectly guess a word, you will lose a life." 
    print "The score at the end of the game, is used to determine the winner." 
    print "" 
    print "Would you like to use a randomly generated word, or input your own?" 
    print "Enter 'R' for randomly generated word, or 'I' for your own input." 
    decision_bool = False 
    decision_length = False 
    while decision_bool == False: #While decision_bool equals "False", do below. 
     while decision_length == False: #While decision_length equals "False", do below. 
      decision = raw_input("") 
      if len(decision) == 1: #If the length of decision eqausl 1, do below. 
       decision_length = True 
       decision = decision.capitalize() #Capitalize the string value of decision. 
       if decision == "R": #if the value of decision, eqauls "R". 
        print "You chose randomly generated word." 
        print "" 
        print "Forwarding..." 
        decision_bool = True 
        print "" 
        rand_word() #Call the function, rand_word() 
       elif decision =="I": #If decision equals "I", do below. 
        print "You chose to input your own word." 
        print "" 
        print "Forwarding..." 
        decision_bool = True 
        print "" 
        user_word() #Call the function, user_word() 
       else: 
        print "You entered an incorrect value for the question. Try again." 
      else: 
       print "You entered an incorrect value for the question. Try again." 

menu() 
+4

的代碼堆棧溢出問題相當冗長。你應該嘗試找到那些不合適的部分,並專注於此。 – zmbq

+1

另一件事是你的意見。我知道這可能是你寫過的第一批程序之一,但學習永遠不會太早......你的評論大多是微不足道的,從某種意義上說,它們並沒有增加代碼的含義。在guess_part(word)這一行放置一個評論,例如「用函數guess_part調用參數詞」並不能幫助任何人閱讀你的代碼。在編寫代碼時,你應該假設閱讀它的人會知道該語言。 – zmbq

+1

嗨阿什利,歡迎來到StackOverflow。你有什麼問題?您是否可以將代碼縮短到重現問題所需的最少行數?快速拋開:你應該考慮你使用評論的方式和方式。 'while decision == False:#雖然決定等於「假」,下面做。「並不真正有用。評論*爲什麼*你做的事情,不要重複代碼本身已經說得很好。 –

回答

0

下面的代碼可能會回答你的問題:
「我所要做的就是確保當
正確的字母有人類型,將其放置在正確的地方。「
它不驗證輸入。
提示:如果你驗證,我建議你去看看re模塊
由於print語句,如果你使用python 3+這個代碼將無法正常工作:

from random import choice 

def found(s , v): 
    # returns a list of indexes if v is in s 
    # else it returns None 
    return [i for i in range(len(s))if s[i] == v] 

def guess_word(word, mask, lives): 

    # returns a 2 tuple : lettermask and the lives left 
    # just like 'guess_part' but with 2 more para's 
    # to increase flexibility. 
    # Beware !!! this function does not check the para's 

    lettermask=list(len(word)* mask) 
    livesleft ='You have {} lives left .\n' 
    guess='Plz type a char a-z.\n>:' 
    print word # just for checking 
    while mask in lettermask: 
     if lives < 1: break 
     print 'lettermask= '+ ''.join(lettermask) 
     p = livesleft.format(lives) 
     letterguess= raw_input(p + guess) 
     print 'letterguess= '+ letterguess 
     result = found(word, letterguess) 
     if result: 
      print 'Good guess :) ' 
      for i in result : 
       lettermask[i]=word[i] 
     else: 
      print 'Wrong guess :(' 
      lives-=1 
    return lettermask,lives 

random_words = ['extraordinary','happy','computer', 
'python','screen','cheese','cabaret', 
'caravan','bee','wasp','insect','mitosis', 
'electronegativity','jumper','trousers'] 


word=choice (random_words) 
# or word= raw_intput('pls type a word') 
mask='*' 
lives= 6 
resultS,lives = guess_word(word, mask, lives) 
if mask in resultS: 
    print 'Your result: {}'.format("".join(resultS)) 
    print 'You have failed,the word was: {}'.format(word) 
else: 
    print 'Congrats !!! Your score is: {}'.format(lives)