Python-打印更新计数器不工作

Python-打印更新计数器不工作,python,Python,这是我的密码。目标是打印一个计数器,更新同一通道中正在检查的页面的编号,替换旧的编号: import time start_page = 500 stop_page = 400 print 'Checking page ', for n in range(start_page,stop_page,-1): print str(n), time.sleep(5) # This to simulate the execution of my code print '\r

这是我的密码。目标是打印一个计数器,更新同一通道中正在检查的页面的编号,替换旧的编号:

import time

start_page = 500
stop_page = 400

print 'Checking page ',

for n in range(start_page,stop_page,-1):
    print str(n),
    time.sleep(5) # This to simulate the execution of my code
    print '\r',
这不会打印任何内容:

$ python test.py
$ 
我使用的是Python 2.7.10,导致问题的一行可能是
print'\r',
,因为如果我运行这个:

import time

start_page = 500
stop_page = 400

print 'Checking page ',

for n in range(start_page,stop_page,-1):
    print str(n),
    time.sleep(5) # This to simulate the execution of my code
    #print '\r',
我有以下输出:

  $ python test.py 
    Checking page  500 499 498 497 496 495 494 493 492 491 490 489 488 487 486 485 484 483 482 481 480 479 478 477 476 475 474 473 472 471 470 469 468 467 466 465 464 463 462 461 460 459 458 457 456 455 454 453 452 451 450 449 448 447 446 445 444 443 442 441 440 439 438 437 436 435 434 433 432 431 430 429 428 427 426 425 424 423 422 421 420 419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402 401
  $

删除打印表达式后的COMA:

print 'Checking page ',
print str(n),
print '\r',
PS:自从有人问起,首先要注意的是,打印不是一个函数,而是一个语句,因此它的解释方式不同。 特别是在打印情况下,在打印后添加“,”将使其打印内容而不使用换行符

特别是在您的程序中,它所做的是:

printing 'Checking page' -> NO \n here
printing n -> no \n here
printing '\r' -> again no '\n' here
因为没有向输出发送任何新行,所以操作系统没有刷新数据。您可以在打印('\r')之后添加sys.stdout.flush(),如果需要,可以查看它的变化

更多关于打印声明的信息,请点击此处。

为什么我被否决了?o、 o试试这个:

import time

start_page = 500
stop_page = 400

print ('Checking page ')

for n in range(start_page,stop_page,-1):
    print(str(n))
    time.sleep(5) # This to simulate the execution of my code
    print('\r')

我刚把它输入到我的解释器中,它运行得很好。你在运行哪个版本的Python?我也是。您如何尝试运行上述代码?尽管事实上你有一些额外的逗号在那里。。。它应该很好,对我来说很好…@leaf我想是2.x,因为OP没有在
打印
语句周围使用
()
为什么在OPs的情况下逗号是错误的?即使它们不应该在那里,它们会影响代码吗?假设OP使用的是Python 3.x,如果它们使用的是Python 3,它们应该执行
print(n,end='\r')