-
Notifications
You must be signed in to change notification settings - Fork 0
/
bchain.py
69 lines (54 loc) · 1.63 KB
/
bchain.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
blockchain = []
def get_last_value():
""" extracting the last element of the blockchain list """
return blockchain[-1]
def add_value(transaction_amount, last_transaction=None):
if last_transaction is None:
last_transaction = [1]
blockchain.append([last_transaction, transaction_amount])
def get_transaction_value():
user_value = float(input('Enter your transaction amount '))
return user_value
def get_user_choice():
user_input = input("Please give your choice here: ")
return user_input
def print_block():
for block in blockchain:
print("Here is your block")
print(block)
def verify_chain():
index = 0
valid = True
for block in blockchain:
if index == 0:
index += 1
continue
elif block[0] == blockchain[index - 1]:
valid = True
else:
valid = False
break
index += 1
return valid
tx_amount = get_transaction_value()
add_value(tx_amount)
while True:
print("Choose an option")
print('Choose 1 for adding a new transaction')
print('Choose 2 for printing the blockchain')
print('Choose 3 if you want to manipulate the data')
print('Choose anything else if you want to quit')
user_choice = get_user_choice()
if user_choice == 1:
tx_amount = get_transaction_value()
add_value(tx_amount, get_last_value())
elif user_choice == 2:
print_block()
elif user_choice == 3:
if len(blockchain) >= 1:
blockchain[0] = 2
else:
break
if not verify_chain():
print('Block-Chain manipulated')
break