2017-10-05 107 views
-4

基本上,這是我迄今爲止的hang子手遊戲的代碼。我需要一些幫助。所以首先如果有人可以通過添加可能的東西來修改我的代碼。 1)我不知道如何用下劃線替換字母。所以每次用戶輸入正確的字母時,都應該替換正確的下劃線。 2)如果人猜錯字母,我不知道如何減少生命。 注意:我打印了實際的文字用於測試,稍後我將刪除它。Python Hang子手替換字母

import time 
import random 

#words 

simpWords = ['triskaidekaphobia', 'spaghettification', 'sesquipedalian', 'floccinaucinihilipilification', 'deipnosophist'] 
medWords = ['erubescent', 'entomophogy', 'noctambulist', 'parapente', 'umbriferous'] 
hardWords = ['cat', 'house', 'degust', 'glaikit', 'otalgia'] 

simpWordsR = random.choice(simpWords) 
medWordsR = random.choice(medWords) 
hardWordsR = random.choice(hardWords) 


#welcome the user 

name = input("What is your name?") 
print ("Hello! " + name + ". Time to play Hangman") 

#wait for 1 second 
time.sleep(1) 

print ("") 

correctLetters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' 

#picking levels 

gameMode = input('Choose a level - Easy (10 Guesses), Medium (8 Guesses) or Hard (7 Guesses)') 
if gameMode == str('easy'): 
    numberOfGuesses1 = 11 
    print ('') 
    print (list(simpWordsR)) 
    blanks = '_ ' * len(simpWordsR) 
    correctLetters = '' 
    for i in range(len(simpWordsR)): # replace blanks with correctly guessed letters 
    if simpWordsR[i] in correctLetters: 
     blanks = blanks[:i] + simpWordsR[i] + blanks[i+1:] 
    print (blanks) 

elif gameMode == str('medium'): 
    numberOfGuesses2 = 8 
    print ('') 
    print (list(medWordsR)) 
    blanks = '_ ' * len(medWordsR) 
    correctLetters = '' 
    for i in range(len(medWordsR)): # replace blanks with correctly guessed letters 
    if medWordsR[i] in correctLetters: 
     blanks = blanks[:i] + medWordsR[i] + blanks[i+1:] 
    print (blanks) 

elif gameMode == str('hard'): 
    numberOfGuesses3 = 7 
    print ('') 
    print (list(hardWordsR)) 
    blanks = '_ ' * len(hardWordsR) 
    correctLetters = '' 
    for i in range(len(hardWordsR)): # replace blanks with correctly guessed letters 
    if hardWordsR[i] in correctLetters: 
     blanks = blanks[:i] + hardWordsR[i] + blanks[i+1:] 
    print (blanks) 

time.sleep(1) 

print ("") 

numberOfGuesses1 -= 1 
print (numberOfGuesses1) 

while numberOfGuesses1 == 10: 
    guess = input("Guess a Character!") 

    if (guess in simpWordsR): 
    print ("Well Done! You Guessed it right!") 

    else: 
    print ("The letter is not in the word. Try Again!") 


if numberOfGuesses1 == 0: 
    print ("Game Finished. Maybe Try Again y/n.") 

非常感謝您的幫助。我其實是一個Python編程的初學者。我嘗試了其他例子,但由於某種原因,它不適用於我的代碼,並且我更改了變量。

+1

請發表您的代碼 –

+0

歡迎SO。不要張貼圖片的代碼或鏈接到代碼。你的問題。將代碼複製並粘貼到您的問題中。訪問[幫助]並閱讀[問]。 –

+0

我已經發布了它 – mysticalstudies

回答

1

見註釋代碼:

import time 
import random 

#words 

simpWords = ['triskaidekaphobia', 'spaghettification', 'sesquipedalian', 'floccinaucinihilipilification', 'deipnosophist'] 
medWords = ['erubescent', 'entomophogy', 'noctambulist', 'parapente', 'umbriferous'] 
hardWords = ['cat', 'house', 'degust', 'glaikit', 'otalgia'] 

# Just use one word, which will be set after user selects difficulty 
#simpWordsR = random.choice(simpWords) 
#medWordsR = random.choice(medWords) 
#hardWordsR = random.choice(hardWords) 


#welcome the user 

name = input("What is your name?") 
print ("Hello! " + name + ". Time to play Hangman") 

#wait for 1 second 
time.sleep(1) 

print ("") 

# Removed uppercase. We will only use lower case 
alphabet = 'abcdefghijklmnopqrstuvwxyz' 

#picking levels 

gameMode = input('Choose a level - Easy (10 Guesses), Medium (8 Guesses) or Hard (7 Guesses)').lower() 
if gameMode == 'easy': 
    numberOfGuesses = 11 
    theWord = random.choice(simpWords) 
    # Not sure what this is for....deleted 
    #correctLetters = '' 
    #for i in range(len(simpWordsR)): # replace blanks with correctly guessed letters 
    # if simpWordsR[i] in correctLetters: 
    # blanks = blanks[:i] + simpWordsR[i] + blanks[i+1:] 
    #print (blanks) 

elif gameMode == 'medium': 
    numberOfGuesses = 8 
    theWord = random.choice(medWords) 

else: 
    numberOfGuesses = 7 
    theWord = random.choice(hardWords) 

print(gameMode) 
print(theWord) # For debugging purposes 

# Since python strings are immutable, use a list 
blanks = ['_'] * len(theWord) 

# Need to keep a list of already guessed letters 
used_letters = [] 

time.sleep(1) 

print ("") 

# Move this to the loop 
#numberOfGuesses1 -= 1 
#print (numberOfGuesses1) 

#while numberOfGuesses1 == 10: 
while numberOfGuesses > 0: 
    print (numberOfGuesses) 
    print (' '.join(blanks)) 

    # get user input and convert to lower case 
    guess = input("Guess a Character!").lower() 

    # Make sure it's a letter 
    if not guess in alphabet: 
     print("Enter a letter...") 
    # Make sure not already guessed 
    elif guess in used_letters: 
     print("Already guessed that....") 
    # Valid guess. Check it 
    else: 
     used_letters.append(guess) 
     if (guess in theWord): 
      print ("Well Done! You Guessed it right!") 
      # Loop through and replace 
      for x in range(0, len(theWord)): 
       if theWord[x] == guess: 
        # Note: this works since theWord and blanks have the same length 
        blanks[x] = guess 
      # Check for success 
      if not '_' in blanks: 
       print("You win") 
       # End the loop 
       break 
     else: 
      print ("The letter is not in the word. Try Again!") 
      # Only decrement if incorrect 
      numberOfGuesses -= 1 

print ("Game Finished. Maybe Try Again y/n.") 
+1

'numberOfGuesses'只能在不正確的猜測時遞減。這意味着它不能被用作唯一的循環條件。 – Mark

+0

@Mark。有可能。這取決於遊戲設計師。 –

+0

來自OP:「如果一個人猜錯了錯誤的字母,我不知道該怎麼減少生命。」 – Mark