Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python, write a function called creditCardCount() that accepts one string rep

ID: 3792397 • Letter: I

Question

In Python, write a function called creditCardCount() that accepts one string representing the name of a credit card.  

This function should open the transactions file and record all 80 transaction in a list of lines. Place the section in which you open the file inside a try block where you check for IOError and FileNotFoundError. If either of those exceptions are generated, output "Unable to open file" and return a -1.

You'll need to use the split() function. Note that the fields are separated by commas (not by spaces). The type of credit credit card that was used is contained in the 4th field.

Your function should return the number of transactions in which the card was used.

Ask the user to name a credit card. Tell them it should be either Visa, Mastercard, or Amex. Then invoke your function passing as the argument, whatever card the user entered. This is shown in the examples below.

Use the value returned by the function inside a nicely formed output statement as shown below.

Here are some sample runs. Note: The values returned in my examples here are the correct values. For example, you should get a value of 38 for the number of transactions that used Visa, 6 for Amex, and 0 for an unrecognized card.

Note: Do NOT simply do a search on the line for the credit card name. Some lines have credit cards listed elsewhere in the line, unrelated to the type of transaction.For example, at the end of line #1, you'll see a few credit cards referenced that have nothing to do with the transaction.

Which card would you like to search for? Visa, Mastercard,Amex: Visa There are 38 transactions that used a Visa card. RESTART C: /Dropbox/241/mendelsohn a5.py which card would you like to search for? Visa, Mastercard,Amex: Amex There are 6 transactions that used a Amex card. RESTART C: Dropbox /241/mendelsohn a5.py Which card would you like to search for? visa, MasterCard, Amex German Express There are 0 transactions that used a GermanExpress card.

Explanation / Answer

#!/usr/bin/env python
import re
def crediCardCount(cardtype,word):
        count=0
        if re.match(cardtype, word):
                count = count+1
        return count
try:
                
    transfile = open("C:UserssreenuDesktop\text.txt", "r")
    cardtype = raw_input("Which card would you like to search for? Visa, Mastercard,Amex:")
    num = 0
    for line in transfile:
        word = []
        word.append(line.split(','))
        num = num+crediCardCount(cardtype,word[0][3])             
    print "There are ",num," transactions that used a ",cardtype," card."
except:
    print "Unable to open file"

Note : I am using function creditCardCount() for knowing the count of transactions and also using split() for split the strings in each line of the file. I created text file but i'm unable send you that lines of text file mentioned below please copy data into text file and give name as text.txt and change the path according to your system.

The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Mastercard
The,type,card,Visa
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Amex
The,type,card,Amex
The,type,card,Mastercard
The,type,card,Mastercard
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Mastercard
The,type,card,Visa
The,type,card,Amex
The,type,card,Visa
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Mastercard
The,type,card,Visa
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Visa
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Mastercard
The,type,card,Visa
The,type,card,Visa
The,type,card,Visa
The,type,card,Amex
The,type,card,Mastercard
The,type,card,Mastercard
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Mastercard
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Mastercard
The,type,card,Visa
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa
The,type,card,Visa
The,type,card,Amex
The,type,card,Amex
The,type,card,Mastercard
The,type,card,Mastercard
The,type,card,Amex
The,type,card,Amex
The,type,card,Amex
The,type,card,Visa

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote