Python Hangman输出

时间:2014-12-05 03:20:26

标签: python if-statement while-loop func function

当我尝试运行程序时,输出时会被问到"到目前为止,你已经猜到了#34;输出与提示不在同一行而不是在它下面的行。任何方法来解决这个问题?程序中还有其他任何应该修复的错误吗?

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 个答案:

答案 0 :(得分:1)

如果您想在print语句后省略换行符,只需使用print("string", end="")