2016-04-28 21 views
2

リスト内のリストを印刷しようとしています。私は知っている、私は前に同様の質問を投稿した - Trouble printing a list within a list、これは同じトピック内の別の問題ですので、重複としてこれをフラグしないでください。私は2つのコードを一緒にマージして、これは最終的なコードです。 if文では、ユーザが入力した特定の顧客の詳細をとして出力しようとしています。コードはmyListで検索され、Payments.txtが追加されています。このコードはすべてこれを行うことができますが、問題は複数回これを出力することです。正確には6回:リスト内のリストを印刷する

[['E1234', '12/09/14', '440', 'A', '0']] 
Customer number: E1234 
Date of payment: 12/09/14 
Payment Amount: 440 
Paid Amount: 0 

は、これは私のコードです:

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 scores: ") 
    record = myFile.readlines() 
    for line in record: 
     myList.append(line.strip().split(',')) 
     details = [x for x in myList if x[0] == customer_number] 
     if details: 
      print(details) 
      print("Customer number: ", details[0][0]) 
      print("Date of payment: ", details[0][1]) 
      print("Payment Amount: ", details[0][2]) 
      print("Paid Amount: ", details[0][4]) 
    myFile.close() 
    print(myList) 

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

'elifの決定== "Q" または "qは":'おそらくべきである 'elifの決定== "Q" または決定== "Q":' ' – alecxe

+0

セットでの意思決定( 'QQ') ' –

+2

または' elif decision.lower()== 'q': ' – SQLnoob

答えて

3

あなたは答えを見つけると、印刷時にループを終了する必要があります。

if details: 
    print(details) 
    print("Customer number: ", details[0][0]) 
    print("Date of payment: ", details[0][1]) 
    print("Payment Amount: ", details[0][2]) 
    print("Paid Amount: ", details[0][4]) 
    break 
+0

ありがとう、それを実現する必要があります! – User0123456789

関連する問題