2016-04-27 11 views
2

これら2つのコードを結合して、最終的な部分がテキスト文書 "Payments"をリストに追加できるようにしようとしています。 「支払い」の各ラインについて、私はmyList内のリストの中にそれをしたいので、このようなものになります。if文の下で2つのforループをマージする

myList = [['E1234','12/09/14','440','A','0']['E3431','10/01/12','320','N','120']['E0987','04/12/16','342','A','137']] 

私は顧客を入力するようユーザーに促すために、最終的なコードを作成できるようにしたいが番号をmyListまで検索し、顧客のすべての情報を画面に表示します。

これは2つのプログラムのうちの最初のプログラムです。これは最終コードの「バックボーン」です。これをAとしましょう:

print("Option A: Show a record\nOption Q: Quit") 
decision = input("Enter A or Q: ") 

if decision == "A" or decision == "a": 
    myFile = open("Payments.txt") 
    customer_number = input("Enter a customer number to view their details: ") 
    record = myFile.readlines() 
    for line in record: 
     if customer_number in line: 
      print(line) 
    myFile.close() 

elif decision == "Q" or "q": 
    exit 

これは2番目のコードです。これをBとしましょう。

myFile = open("Payments.txt") 
myList = [] 
for item in myFile: 
    print(item.strip()) 
    myList.append(item.strip().split(',')) 
myFile.close() 
print(myList) 

if文にBを挿入したいとします:if decision == "A" or decision == "a":

私はforループについて混乱します。これは、AとBにforループがあり、両方とも最終コードにとって不可欠です。 forループのいずれかを中断することなく、BをAに配置できません。

print("Option A: Show a record\nOption Q: Quit") 
decision = input("Enter A or Q: ") 
myList = [] 

if decision == "A" or decision == "a": 
    myFile = open("Payments.txt") 
    customer_number = input("Enter a customer number to view their details: ") 
    record = myFile.readlines() 
    for line in record: 
     for item in myFile: 
      print(item.strip()) 
      myList.append(item.strip().split(',')) 
      print(myList) 
     if customer_number in line: 
      print(line) 
    myFile.close() 

elif decision == "Q" or "q": 
    exit 

お客様番号の元の行が表示されますが、リストは印刷されません。

アップデート私は別に、各ラインから個々のデータを印刷することができるようにしたい

:あなたのコメントで

Customer number: E1234 
Date of payment: 12/09/14 
Payment amount: £440 
Paid amount: £0 
+0

私が正しく理解していれば、単に特定の顧客の詳細情報を出力しますが、Bは何をするのでしょうか?Payments.txtの内容を印刷するだけですか? – Bijan

+0

Bは、Payments.txtの各行をmyList内の個々のリストに追加します。私は、Aのifステートメントの中にBを入れて、PaymentsをmyListに追加し、リストのcustomer_numberを見つけてその特定の行を表示します。 – User0123456789

+0

これはcustomer_numberを含む行を表示することを目的としていますか?なぜmyListで気にするの? – Bijan

答えて

1

あなたを述べリスト私がしているに追加する必要あなたの現在の答えがあなたのAとBの部分を分けていたが、あなたはそれらを結合しなければならなかった。

ロジックは、customer_numberが1行に存在する場合は、リストに追加し、ループを使用してリストを反復処理して、必要なものを印刷します。お客様のソリューションは、顧客の詳細の全ラインを印刷しており、1行おきにも印刷していました。ここで

print("Option A: Show a record\nOption Q: Quit") 
decision = input("Enter A or Q: ").lower() 
myList = [] 

if decision == "a": 
    myFile = open("Payments.txt") 
    customer_number = input("Enter a customer number to view their details: ") 
    record = myFile.readlines() 
    for line in record: 
     if customer_number in line: 
      myList.append(line.strip().replace("'","").split(',')) 
      for info in myList: 
       print("Customer number: ", info[0]) 
       print("Date of payment: ", info[1]) 
       print("Payment Amount: ", info[2]) 
       print("Paid Amount: ", info[4]) 
    myFile.close() 

elif decision == "q": 
    exit() 

が出力されます。

enter image description here

+0

なぜファイルの内容を2回ループしますか? –

+0

動作しませんでした。それぞれの行を表示し、それが 'myList'に追加していることを示しました。 – User0123456789

+0

ありがとうございました! – User0123456789

0

私ははmyListを検索、その後、 顧客番号を入力するようユーザーに促すために、最終的なコードを作成できるようにしたいです得意先コード を入力し、得意先のすべての情報を画面に表示します。

私はあなたのコードをいくつか変更して、PEP-8準拠の定型詞に入れました。

def customer_payment(customer_number): 
    """Edited Code. Purpose is to accept User Input for a customer number 
    Search for Customer Number, and if Found, Display Information about Customer. 
    """ 
    myList = [] 
    with open("Payments.txt") as myFile: 
     for line in myFile: 
      if customer_number in line: 
       print(line) 
      for item in line: 
       print(item.strip()) 
       myList.append(line.strip().split(',')) 


def main(): 
    decision = input("Option A: Show a record\nOption Q: Quit\nEnter A or Q: ") 
    if decision.lower() == "a": 
     customer_number = input("Enter a customer number to view their details: ") 
     customer_payment(customer_number) 

if __name__=="__main__": 
    main() 

これはPayments.txtはであるフォーマットに基づいて修正する必要があるかもしれない。

+0

それはPayments.txtのすべての文字をそれ自身の行に印刷しました... – User0123456789

関連する問題