Python在print语句之前添加空格

时间:2015-06-14 11:02:48

标签: python

我的python代码:

guess = 50
high = 99
low =0
hilo = 0
print ('Please think of a number between 0 and 100!')
while hilo != 'c':
    print ('Is your secret number ' + str(guess) + ' ?')
    print ("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low."),
    hilo = raw_input ("Enter 'c' to indicate I guessed correctly. ")
    if (hilo != 'c' and hilo != 'h' and hilo != 'l'):
        print 'Sorry, I did not understand your input.'
    if hilo == 'l':
        low = guess
        guess = (high + low) / 2
    if hilo == 'h':
        high = guess
        guess = (high + low) / 2
print ('Game over. Your secret number was: ' + str(guess))

打印出来:

Please think of a number between 0 and 100!
Is your secret number 50 ?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'c' to indicate I guessed correctly. h
 Is your secret number 25 ?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'c' to indicate I guessed correctly. l
 Is your secret number 37 ?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.
Enter 'c' to indicate I guessed correctly.

以下代码行的第一次执行与输出的其余部分对齐,但所有后续执行都在前面有一个空格。

print ('Is your secret number ' + str(guess) + ' ?')

是什么造成的?

2 个答案:

答案 0 :(得分:0)

改为运行它,你在行尾有一个逗号(在第三个print语句中):

guess = 50
high = 99
low =0
hilo = 0
print ('Please think of a number between 0 and 100!')
while hilo != 'c':
    print ('Is your secret number ' + str(guess) + ' ?')
    print ("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low.")
    hilo = raw_input ("Enter 'c' to indicate I guessed correctly. ")
    if (hilo != 'c' and hilo != 'h' and hilo != 'l'):
        print 'Sorry, I did not understand your input.'
    if hilo == 'l':
        low = guess
        guess = (high + low) / 2
    if hilo == 'h':
        high = guess
        guess = (high + low) / 2
print ('Game over. Your secret number was: ' + str(guess))

答案 1 :(得分:0)

是的,这是一个特殊的Canopy问题,因为它没有在其他IDE中复制。已经通知了Canopy技术支持,他们正在制定解决方案。