2014-12-05 104 views
0

當我試着運行程序輸出時,當被問到「到目前爲止你猜到了」輸出與提示不在同一行,而是在它下面的行上。任何方法來解決這個問題?程序中是否還有其他錯誤需要解決?Python Hang子手輸出

HANGMANPICS = [''' ''', ''' 


    | 




''', ''' 


    | 
    0 



''', ''' 


    | 
    0 
    | 


''', ''' 


    | 
    0 
    /| 


''', ''' 


    | 
    0 
    /|\ 


''', ''' 


    | 
    0 
    /|\ 
/ 

''', ''' 


    | 
    0 
    /|\ 
/\ 

'''] 

def getSecret_Word(): 
while True: 
    wordList =input('Please enter a word to be guessed\nthat does not contain ? or white space: ') 
    wordList = wordList.strip() 
    if ' ' and '?' not in wordList: #makes sure that the word is valid 
     break 
return wordList #returns back to wordList if invalid 

def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord): 
print(HANGMANPICS[len(missedLetters)]) 
print() 
blanks = '?' * len(secretWord) 

for i in range(len(secretWord)): 
    if secretWord[i] in correctLetters: 
     blanks = blanks[:i] + secretWord[i] + blanks[i+1:] 

for letter in blanks: 
    print(letter, end='') 
    print() 


print('So far you have guessed:', ', '.join(sorted(missedLetters+correctLetters))) 
print() 



def getGuess(alreadyGuessed): 
    while True: 
    guess = input() 
    guess = guess.lower() 
    if len(guess) != 1: 
     print('You can only guess a single character.') 
    elif guess in alreadyGuessed: 
     print('Please enter your next guess:\nYou have already guessed the character:',guess) 
    elif guess.isspace(): 
      print('You must enter a guess.') 
    else: 
     return guess 

def playAgain(): 
    print('Do you want to play again? (yes or no)') 
    return input().lower().startswith('y') 


missedLetters = '' 
correctLetters = '' 
secretWord = getSecret_Word() 
gameIsDone = False 

while not gameIsDone: 
    displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord) 

guess = getGuess(missedLetters + correctLetters) 

if guess in secretWord: 
    correctLetters = correctLetters + guess 


    foundAllLetters = True 
    for i in range(len(secretWord)): 
     if secretWord[i] not in correctLetters: 
      foundAllLetters = False 
      break 
    if foundAllLetters: 
     print('You correctly guessed the secret word: ', secretWord) 
     gameIsDone = True 
else: 
    missedLetters = missedLetters + guess 


    if len(missedLetters) == len(HANGMANPICS) - 1: 
     print(HANGMANPICS[len(missedLetters)]) 
     print('You failed to guess the secret word:',secretWord) 
     gameIsDone = True 


if gameIsDone: 
     break 

回答

1

如果你想省略print聲明後的換行,只需使用print("string", end="")

+0

謝謝。該計劃中的任何其他問題? – DatOneGuy 2014-12-05 03:45:15

+0

@DatOneGuy「謝謝。程序中有其他問題嗎?」我認爲你對這個網站有錯誤的印象。它的目的是回答特定的問題,而不是爲您查看代碼。有時你可能會得到提示,但你不能問這個問題,否則你的問題可能會被關閉 – jamylak 2014-12-05 04:25:53