如何删除Python中raw_input上面的空行?

时间:2015-03-17 07:56:30

标签: python python-2.7 formatting

我有一段时间的陈述:

print 'Word', len(guessMe), 'letters long.'
print '------------- '
while verify != [] and guessesLeft > 0: 
  print 'You have', guessesLeft, 'guesses left.'
  userGuess = raw_input('Please guess a letter: ')
  userGuess = userGuess.lower()
  userGuessSingleton = list(userGuess)
  userGuessList = userGuessList + userGuessSingleton

  p = lettersList.index(userGuess) 
  del(lettersList[p]) 
  letters = "".join(lettersList) 
  print 'Available letters: ', letters

输出:

Word is 10 letters long.
---------------
You have 13.0 guesses left.

Please guess a letter: a

为什么其他打印声明不会产生此问题?它与raw_input()有关吗?如何在'您还有13.0猜测后删除该空白行'

注意: Nsh的recommendation无法正常工作,因为它会产生另一个问题:"Please guess a letter: a"之后行上的一个空格缩进

Please guess a letter: a
 Letters are 'abc'

6 个答案:

答案 0 :(得分:2)

简而言之,您的代码不应该按照您声称的方式执行。如果这是您的所有可能影响输出的代码,那么raw_input之前的新行仅对您的情况是唯一的。我已经用Ubuntu,OS X和Windows 8测试了你的代码,这是我得到的输出:

Word 10 letters long.
------------- 
You have 13.0 guesses left.
Please guess a letter: 

没有换行。这可能是一个python bug。

答案 1 :(得分:1)

我在python 2中检查你的代码.3。print和raw_input之间没有换行。但是你可以使用一个','将两个印刷组合成一行:

print "This",
print "is",
print "a test"

答案 2 :(得分:1)

我有同样的问题,这是Enthought Canopy编辑器的一个问题。这也是这个问题的重复:Why is there a blank line between my print and raw_input lines?

答案 3 :(得分:1)

实际上,我使用输入为Anaconda / Spyder Python 3.5中的以下代码提供了相同类型的输出:

print("Welcome to the game, Hangman!")
print("I am thinking of a word that is " + str(len(secretWord)) + " letters long.")
print("-------------")
while guessesLeft > 0:
    print("You have " + str(guessesLeft) + " guesses left.")
    print("Available Letters: " + getAvailableLetters(lettersGuessed))
    guess = input("Please guess a letter: ")
    guessInLowerCase = guess.lower()

输出:

Welcome to the game, Hangman!
I am thinking of a word that is 6 letters long.
-------------
You have 8 guesses left.
Available Letters: abcdefghijklmnopqrstuvwxyz

Please guess a letter:

答案 4 :(得分:0)

你使用canpy作为IDLE吗? 我认为你正在使用IDLE的错误。

答案 5 :(得分:0)

你可以改变这一行:

print("Available Letters: " + getAvailableLetters(lettersGuessed))

print("Available Letters: " + getAvailableLetters(lettersGuessed), end="")