2016-10-06 9 views
-1

でファイルTXTを読んで、スクリプトによって読み込まれていないと正直に私は理由を知りません。入力バリデーションループ/私の最初の問題は、私のファイルであるPythonの

第2の問題は、私のループ "y"または "Y"がユーザのアカウント入力の質問をループしていないことです。終了するには "n"または "N"それは働く。 (だから私のループはその意味で正しく動作すると思います)

私の現在の状況 私はアカウント番号を読む必要があるtxtファイルを持っています。 アカウント番号が間違っていても、ループしてアカウント番号の検証を依頼する必要があります。

私は私の必要な出力----------------

------------出力はこの

ようになりたいです

>>> 
Enter the account number to be validated: 456321 
Account number 456321 is not valid. 
Enter another card number? Enter Y or N: Y 
Enter the account number to be validated: 5552012 
Account number 5552012 is valid. 
Enter another card number? Enter Y or N: N 
>>> 

-----------エンド必要な出力--------------

-----------私の出力現時点では---------

>>> 
============ RESTART: G:\Software Design\accounts.py ============ 
Enter the account number to be validated: 4453221 
Account number 4453221 is not valid 
Enter another card number? Enter Y or N: y 
Enter another card number? Enter Y or N: n 
>>> 

----------終了電流出力---あなたのループに関して--------

--------マイコード----------

def main(): 

    #setting loop control 
    another = 'y' 

    try: 
     # open file READ ONLY charge_accounts.txt 
     infile = open('charge_accounts.txt', 'r') 

     # Setting accountNum variable 
     accountNum = int(input('Enter the account number to be validated: ')) 

     if accountNum in infile: 
      accountNum = int(infile.readline(accountNum)) 
      print('Account number ' + str(accountNum) + ' is valid') 
     else: 
      print('Account number ' + str(accountNum) + ' is not valid') 
      # Loop controls for other account inputs 
      while another == 'y' or another == 'Y': 
       # Get another account 
       another = input('Enter another card number? ' + 'Enter Y or N: ') 
       if another == 'n' or another == 'N': 
        break 

     infile.close() 

    # Extra credit +5 points (catching errors) 
    except IOError: 
     print('An error occured trying to read') 
     print('the file', charge_accounts.txt) 
main() 

答えて

2

、あなたがなりたいすべてのもの繰り返す必要があります。だから、ループのwhile another == y:体が繰り返されます、あなたが書いたものを考えます。だから、私の助言は、のループを開始するについて考えることです。

accountNum in infile:は、accountNumbintに変換し、文字列を維持する必要があるため、常に偽になります。別のヘッドアップです:

if accountNum in infile: 
    accountNum = int(infile.readline(accountNum)) 
    print('Account number ' + str(accountNum) + ' is valid') 

とにかく思うように動作しません。 fileオブジェクトに何かがあるかどうかを確認すると、ストリームの位置が最後に移動します。つまり、readlineの場合、空の文字列が返されます。あなたがそれについて考えるなら、とにかくreadlineは本当に必要ありません。

+0

これは私には意味がありません.....あなたはダムそれを少しダウンできますか? は、私はあなたが正しい方向に私を指すようにそこにヒントを投げているが、私は私のwhile文を移動したときに私のスクリプトが無限の表情に行き、ちょうど私はちょうどすべてをしようと時間を費やしていない有効なノンストップ.... –

+0

を掲示保持知っていますあなたは上で述べたが、私はそれを取得していない –

+0

@SlowPoke 'accountNum = int(入力( '検証するアカウント番号を入力してください:'))' –

関連する問題