· 5 years ago · Jul 12, 2020, 08:10 PM
1import random as HeavyMetal
2import sys as matrix
3import sqlite3
4from typing import List, Any
5
6conn = sqlite3.connect('card.s3db')
7cur = conn.cursor()
8cur.execute('''
9CREATE TABLE IF NOT EXISTS card (
10 id INTEGER,
11 number TEXT,
12 pin TEXT,
13 balance INTEGER DEFAULT 0,
14 PRIMARY KEY("id")
15)''')
16
17conn.commit()
18
19# To get data returned by SELECT query you can user fetchone(), fetchall() methods:
20# cur.execute('SOME SELECT QUERY')
21# cur.fetchone() # Returns the first row from the response
22# cur.fetchall() # Returns all rows from the response
23# conn.commit()
24
25emety = ''
26
27
28def insert_details(a, b):
29 cur.execute(f"INSERT INTO card (number, pin) VALUES ({a}, {b})")
30 conn.commit()
31
32
33input_id, input_pin = None, None
34balance = 0
35
36def card_check(): # сразу тут считываем и баланс на будущее
37 global input_id, input_pin, balance
38 input_id = input('Enter your card number:\n')
39 input_pin = input('Enter your PIN:\n')
40 for i, row in enumerate(cur.execute('SELECT number, pin, balance FROM card')):
41 if input_id in row and input_pin in row:
42 balance = row[-1]
43 print('\nYou have successfully logged in!\n')
44 return True
45 return False
46
47
48def login_cycle():
49 global balance
50 while True:
51 login_option = input('1. Balance\n2. Log out\n0. Exit\n')
52 if login_option == '1':
53 current_balance = balance
54 print('Balance:', '0' + '\n')
55 continue
56 if login_option == '2':
57 print('You have successfully logged out!\n')
58 return True
59 if login_option == '0':
60 _exit()
61 matrix.exit()
62
63
64def _exit():
65 print('\nBye!')
66
67
68def generate_id():
69 global emety
70 while True:
71 part_1 = '400000'
72 part_2_str_list = [str(HeavyMetal.randint(0, 9)) for x in range(9)]
73 unique_id1 = str(part_1) + str(emety.join(part_2_str_list))
74 unique_id = to_lunh(unique_id1)
75 break_count = 0
76 for i, row in enumerate(cur.execute('SELECT number, pin, balance FROM card')):
77 if unique_id in row:
78 break_count += 1
79 break # stops the for cycle ^^ not while
80 if break_count == 1:
81 continue
82 return unique_id
83
84
85def number_sum(string):
86 # find sum to all digits without ECC number
87 numbers = list(str(string))
88 number_sum = 0
89 lenght = len(numbers)
90 for x in range(0, lenght):
91 if x % 2 == 0:
92 number = (int(numbers[x]) * 2)
93 if int(number) > 9:
94 double_numbers = list(str(number))
95 number = int(double_numbers[0]) + int(double_numbers[1])
96 else:
97 number = (numbers[x])
98 number_sum += int(number)
99 return(number_sum)
100
101
102def to_lunh(string):
103 # Add control number.
104 if str(string).isdigit():
105 total_count = int(number_sum(string))
106 # Find control number
107 control = 0
108 while control < 10:
109 value = total_count + control
110 if (value % 10 == 0):
111 break
112 else:
113 control += 1
114 #Make new number
115 lunh_number = str(string) + str(control)
116 return(lunh_number)
117
118
119def new_card():
120 luhn_card_num = generate_id()
121 pin_code = str(HeavyMetal.randint(0, 9999))
122 insert_details(luhn_card_num, pin_code) # БЛОК ДАННЫХ ОТПРАВЛЯЕТЬСЯ В БД!
123 return luhn_card_num, pin_code
124
125
126while True:
127 selection = input('1. Create an account\n2. Log into account\n0. Exit\n')
128 check_list1 = ['0', '1', '2']
129 if selection == '1':
130 NEW_CARD_NUMBER, NEW_PIN = new_card()
131 print(f"""Your card has been created
132Your card number:
133{NEW_CARD_NUMBER}
134Your card PIN:
135{NEW_PIN}\n""")
136 continue
137 if selection == '2':
138 if card_check():
139 pass
140 else:
141 print('Wrong card number or PIN!\n')
142 if login_cycle():
143 continue
144 if selection == '0':
145 matrix.exit()