-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem Set 6: Cash and credit
71 lines (57 loc) · 1.41 KB
/
Problem Set 6: Cash and credit
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
#CASH
while True:
cash_input = input("Enter amount: ")
try:
cash = float(cash_input)
if cash > 0:
break
else:
print("Invalid input. Please enter a positive number.")
except ValueError:
print("Invalid input. Please enter a valid number.")
coins = round(cash * 100)
count = 0
while coins > 0:
if coins >= 25:
coins -= 25
count += 1
elif coins >= 10:
coins -= 10
count += 1
elif coins >= 5:
coins -= 5
count += 1
else:
coins -= 1
count += 1
print(count)
#CREDIT
def luhn_algorithm(card_number):
digits = list(card_number[::-1])
total_sum = 0
for i in range(len(digits)):
digit = int(digits[i])
if i % 2 == 1:
digit *= 2
if digit > 9:
digit -= 9
total_sum += digit
if total_sum % 10 == 0:
return True
else:
return False
card_number = input("Enter card number: ")
card_number = card_number.strip()
length = len(card_number)
if length < 13 or length > 16:
print("INVALID")
else:
if luhn_algorithm(card_number):
if card_number[:1] == '4':
print("VISA")
elif card_number[:2] in ["34", "37"]:
print("AMEX")
elif card_number[:2] >= '51' and card_number[:2] <= '55':
print("MASTERCARD")
else:
print("INVALID")