+ -
当前位置:首页 → 问答吧 → 一个疑问

一个疑问

时间:2010-12-11

来源:互联网

本帖最后由 eeeee159963 于 2010-12-11 19:09 编辑
  1. # Credit inquiry program.

  2. import sys

  3. # retrieve one user command
  4. def getRequest():
  5.    
  6.    while 1:
  7.       request = int( raw_input( "\n? " ) )
  8.      
  9.       if 1 <= request <= 4:
  10.          break

  11.    return request

  12. # determine if balance should be displayed, based on type
  13. def shouldDisplay( accountType, balance ):

  14.    if accountType == 2 and balance < 0:     # credit balance
  15.       return 1

  16.    elif accountType == 3 and balance > 0:   # debit balance
  17.       return 1

  18.    elif accountType == 1 and balance == 0:  # zero balance
  19.       return 1

  20.    else: return 0

  21. # print formatted balance data
  22. def outputLine( account, name, balance ):
  23.    
  24.    print account.ljust( 10 ),
  25.    print name.ljust( 10 ),
  26.    print balance.rjust( 10 )

  27. # open file
  28. try:
  29.    file = open( "clients.dat", "r" )
  30. except IOError:
  31.    print >> sys.stderr, "File could not be opened"
  32.    sys.exit( 1 )

  33. print "Enter request"
  34. print "1 - List accounts with zero balances"
  35. print "2 - List accounts with credit balances"
  36. print "3 - List accounts with debit balances"
  37. print "4 - End of run"

  38. # process user request(s)
  39. while 1:

  40.    request = getRequest()   # get user request

  41.    if request == 1:         # zero balances
  42.       print "\nAccounts with zero balances:"
  43.    elif request == 2:       # credit balances
  44.       print "\nAccounts with credit balances:"
  45.    elif request == 3:       # debit balances
  46.       print "\nAccounts with debit balances:"
  47.    elif request == 4:       # exit loop
  48.       break
  49.    else:  # getRequest should never let program reach here
  50.       print "\nInvalid request."

  51.    currentRecord = file.readline()    # get first record

  52.    # process each line
  53.    while ( currentRecord != "" ):
  54.       account, name, balance = currentRecord.split()
  55.       balance = float( balance )
  56.       
  57.       if shouldDisplay( request, balance ):
  58.          outputLine( account, name, str( balance ) )

  59.       currentRecord = file.readline() # get next record

  60.    file.seek( 0, 0 )                  # move to beginning of file
  61.   
  62. print "\nEnd of run."
  63. file.close()                          # close file

  64.                                              
复制代码


第51行的while循环结束条件是什么

作者: eeeee159963   发布时间: 2010-12-11

elif request == 4:       # exit loop
      break

作者: catcat811   发布时间: 2010-12-11