ユーザー入力値をブロックチェーンに追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
blockchain=[] def get_last_blockchain_value(): return blockchain[-1] def add_value(transaction_amount,last_transaction=[1]): blockchain.append([last_transaction,transaction_amount]) tx_amount = float(input("your transaction amount please: ")) add_value(tx_amount) tx_amount = float(input("your transaction amount please: ")) add_value(last_transaction=get_last_blockchain_value(),transaction_amount=tx_amount) tx_amount = float(input("your transaction amount please: ")) add_value(tx_amount,get_last_blockchain_value()) print(blockchain) |
実行するとyour transaction amount please:と出てくるので
任意の数値を入力していきます。
>>>
your transaction amount please: 2.5
your transaction amount please: 8.6
your transaction amount please: 5.2
[[[1], 2.5], [[[1], 2.5], 8.6], [[[[1], 2.5], 8.6], 5.2]]
ユーザー入力を自作関数化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
blockchain=[] def get_last_blockchain_value(): return blockchain[-1] def add_value(transaction_amount,last_transaction=[1]): blockchain.append([last_transaction,transaction_amount]) def get_user_input(): return float(input("your transaction amount please: ")) tx_amount = get_user_input() add_value(tx_amount) tx_amount = get_user_input() add_value(last_transaction=get_last_blockchain_value(),transaction_amount=tx_amount) tx_amount = get_user_input() add_value(tx_amount,get_last_blockchain_value()) print(blockchain) |
>>>
your transaction amount please: 1.3
your transaction amount please: 5.8
your transaction amount please: 5.6
[[[1], 1.3], [[[1], 1.3], 5.8], [[[[1], 1.3], 5.8], 5.6]]
コメントを残す