-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscroungeLedger.py
116 lines (109 loc) · 4.21 KB
/
scroungeLedger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""ECU CSI2108 Assessable Workshop - Blockchain & Cryptocurrencies."""
import string
import random
def _randomGen(category):
"""Generate 10 random letters or numbers."""
length = 10
chars = string.ascii_lowercase
digits = string.digits
if category == "c":
result = ""
counter = 0
while counter < length:
result += random.choice(chars)
counter += 1
return result
if category == "n":
result = ""
counter = 0
while counter < length:
result += random.choice(digits)
counter += 1
return result
def _addTransaction():
"""Add the transaction to "ledger.txt"."""
# Spawn sender variable outside scope of below while loop
sender = ""
# Keep asking for input until it is 100% letters
while (sender.isalpha() is False):
sender = input("From (max 10, letters only): ")
if sender == "":
sender = _randomGen("c")
# Sanitise input
sender = sender.strip()
# Lower case only
sender = sender.casefold()
# Use only the first 10 characters of the input
sender = sender[0:10]
# Confirm the stripped, trimmed input to the user
print("Sending from: " + sender)
# Spawn recipient variable outside scope of below while loop
recipient = ""
# Keep asking for input until it is 100% letters
while (recipient.isalpha() is False):
recipient = input("To (max 10, letters only): ")
if recipient == "":
recipient = _randomGen("c")
# Sanitise input
recipient = recipient.strip()
# Lower case only
recipient = recipient.casefold()
# Use only the first 10 characters of the input
recipient = recipient[0:10]
# Confirm the stripped, trimmed input to the user
print("Sending to: " + recipient)
# Spawn amount variable outside scope of below while loop
amount = ""
# Make sure the input is a number greater than 1
while (amount.isdigit() is False) or (int(amount) < 1):
amount = input("Amount (max 10, digits only): ")
if amount == "":
amount = _randomGen("n")
# Sanitise input
recipient = recipient.strip()
# Use only the first 10 characters of the input
amount = amount[0:10]
print("Amount is: " + amount)
try:
# The ledger file, stored as a variable for easier changes
filename = "ledger.txt"
# Open the file for appending, or create if it does not already exist
ledger = open(filename, mode='a+')
# Pad the sender name to 10 characters with whitespace for formatting
sender10 = sender.ljust(10)
# Write the padded sender to the ledger with the set formatting
ledger.write("|FROM:| " + sender10)
# Pad the recipient to 10 characters with whitespace for formatting
recipient10 = recipient.ljust(10)
# Write the padded recipient to the ledger with the set formatting
ledger.write(" |TO:| " + recipient10)
# Write the amount to the ledger witht he set formatting
ledger.write(" |AMOUNT:| " + amount + "\n")
# Watch out for errors opening the file for writing
except (FileNotFoundError, PermissionError) as e:
# Inform the user that there was an IO error
print("Unable to open " + filename + " for writing. Quitting program.")
# Exit the program, no facility to correct this error
quit()
try:
"""Main program logic."""
# Main runtime logic, inspired by previous assignment
print("CSI2108 \"ScroungeCoin\" Ledger Program.")
print("Press CTRL+C to quit cleanly at any time.")
# Infinite loop unless "no" or KeyboardInterrupt
while True:
# Start an input variable outside scope of below while loop
choice = ""
while (choice != "yes") and (choice != "no"):
choice = input("New transaction? \"yes\" to add: ")
# Basic input sanitising
choice = choice.strip()
# Aggressively convert input to lower case
choice = choice.casefold()
if (choice == "yes"):
_addTransaction()
if (choice == "no"):
quit()
except KeyboardInterrupt:
# Catch keyboard interrupts and close cleanly
quit()