UCONN

UCONN
UCONN

Python Invoicer

  Python Invoicer

File handling is an important part of any application.

Python has several functions for creating, reading, updating, and deleting files.

File Handling

The key function for working with files in Python is the open() function.

The open() function takes two parameters; filename, and mode.

There are four different methods (modes) for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exists


f = open("demofile2.txt", "a")

f.write("Now the file has more content!")

f.close()


#open and read the file after the appending:

f = open("demofile2.txt", "r")

print(f.read())


In Python, you can assign a formatted print statement to a variable using f-strings (formatted string literals):

ame = "Alice"

age = 30

formatted_string = f"Hello, my name is {name} and I am {age} years old."


Calculate the invoice and print each line out to a file called lastname.txt




==============================

#!/usr/bin/python3


cust_first_name = input("Customer First name: ")

cust_last_name = input("Customer Last name: ")

file_ext = ".txt"

inv_name = cust_last_name + file_ext

f = open(inv_name, "a")

cust_address = input(" Address: ")

cust_city = input(" City: ")

cust_state = input(" State: ")

cust_zip = input(" Zip: ")


print(cust_last_name)

print(cust_address)

items = {"plates" : 3, "cups" : 1, "glasses" : 2}

for x in items:

    print(x, items[x])

no_plates = input("Number of Plates: ")

no_cups = input("Number of Cups: ")

no_glasses = input("Number of Glasses: ")

total_plates = int(no_plates) * int(items["plates"])

total_cups = int(no_cups) * int(items["cups"])

total_glasses = int(no_glasses) * int(items["glasses"])

total_charge = int(total_plates) + int(total_cups) + int(total_glasses)

inv_name = cust_first_name + " " + cust_last_name + "\n"

print(inv_name)

f.write(inv_name)

inv_address = cust_address + "\n"

print(cust_address)

f.write(inv_address)

print(cust_city)

inv_city = cust_city + "\n"

f.write(inv_city)

print(cust_state)

inv_state = cust_state + "\n"

f.write(inv_state)

print(cust_zip)

inv_zip = cust_zip + "\n"

f.write(inv_zip)


## line item for plates

price_plates = items["plates"]

inv_plates = f"You Bought {no_plates} number of plates at ${price_plates:.2f} = ${total_plates:.2f} dollars. \n"

f.write(inv_plates)

myplates = "You Bought {} number of plates at {} = {} dollars."

print(myplates.format(no_plates, items["plates"], total_plates))


## line item for cups

price_cups = items["cups"]

inv_cups = f"You Bought {no_cups} number of cups at ${price_cups:.2f} = ${total_cups:.2f} dollars. \n"

f.write(inv_cups)

mycups = " {} number of cups at {} = {} dollars."

print(mycups.format(no_cups, items["cups"], total_cups))


## line item for glasses

price_glasses = items["plates"]

inv_glasses = f"You Bought {no_glasses} number of glasses at ${price_glasses:.2f} = ${total_glasses:.2f} dollars. \n"

f.write(inv_glasses)

myglasses = " {} number of glasses at {} = {} dollars."

print(myglasses.format(no_glasses, items["glasses"], total_glasses))


print('==============================================================')

inv_total = f" Total Amount Due = ${total_charge:.2f} dollars. \n"

f.write("============================================================== \n")

f.write(inv_total)


mytotal = " Total Amount Due = {} dollars."

print(mytotal.format(total_charge))

f.close()

=========================================================

Results

john_iacovacci1@cloudshell:~/pyinv (uconn-engr)$ python3 invoicer.py

Customer First name: John

Customer Last name: Iacovacci

 Address: 55 Daffodil Road

 City: Stamford

 State: CT

 Zip: 06903

Iacovacci

55 Daffodil Road

plates 3

cups 1

glasses 2

Number of Plates: 4

Number of Cups: 4

Number of Glasses: 4

John Iacovacci


55 Daffodil Road

Stamford

CT

06903

You Bought 4 number of plates at 3 = 12 dollars.

 4 number of cups at 1 = 4 dollars.

 4 number of glasses at 2 = 8 dollars.

=========================================================

 Total Amount Due = 24 dollars.


john_iacovacci1@cloudshell:~/pyinv (uconn-engr)$ cat Iacovacci.txt

John Iacovacci

55 Daffodil Road

Stamford

CT

06903

You Bought 4 number of plates at $3.00 = $12.00 dollars. 

You Bought 4 number of cups at $1.00 = $4.00 dollars. 

You Bought 4 number of glasses at $3.00 = $8.00 dollars. 

============================================================== 

 Total Amount Due = $24.00 dollars. 

john_iacovacci1@cloudshell:~/pyinv (uconn-engr)$ 


Attach code and file to an email


Download file


Click on 3 dots on editor bar




Click download

Click on Folder icon to browse directory structure



Click on arrow to browse



Click arrow of directory where file is in


Select file


Click Download



Attach file and send both code and file in email




No comments:

Post a Comment

Disable Billing

Search for Billing Manage billing accounts Go to MYPROJECTS CLICK ON THE 3 BUTTON Actions Then hit disable