Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab 7 completed #143

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions code/daniel/python_files/lab07-ccv/lab07-ccv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Lab 7: Credit Card Validation

# Let's write a function credit_card_validator which returns whether a string containing a credit card number is valid as a boolean. The steps are as follows:

# Convert the input string into a list of ints
# Slice off the last digit. That is the check digit.
# Reverse the digits.
# Double every other element in the reversed list (starting with the first number in the list).
# Subtract nine from numbers over nine.
# Sum all values.
# Take the second digit of that sum.
# If that matches the check digit, the whole card number is valid.

# Here is a valid credit card number to test with: 4556737586899855

# For example, the worked out steps would be:

# 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5
# 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5
# 5 8 9 9 8 6 8 5 7 3 7 6 5 5 4
# 10 8 18 9 16 6 16 5 14 3 14 6 10 5 8
# 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8
# 85
# 5
# True Valid!
# ========================================================================================


enter_ccn = (input("Enter your 16-digit credit card number: "))
# print(len(enter_ccn))


try:
len(enter_ccn) == 16
enter_ccn = list(enter_ccn)
print(enter_ccn)
# print("check")
except (ValueError):
print("Invalid number length, try again")
print(enter_ccn)
except (ValueError):
print('Please enter a number')
# ====================================================
# Convert the input string into a list of ints
# ====================================================
# print(enter_ccn)
# print(type(enter_ccn))

# ====================================================
# Slice off the last digit. That is the check digit.
# ====================================================
check_digit = enter_ccn.pop()
print(check_digit)

# ====================================================
# Reverse the digits.
# ====================================================
enter_ccn.reverse()
print(enter_ccn)

# =============================================================================================
# Double every other element in the reversed list (starting with the first number in the list).
# =============================================================================================

index = 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at this section here again and check your print statements in the terminal.

Pay close attention to step #4
Double every other element in the reversed list (starting with the first number in the list

Make sure to check your answers against the answers from the lab

ccn_odd_doubled = []
for odd in enter_ccn:
if int(enter_ccn[index]) % 2 != 0:
ccn_odd_doubled.append(int(enter_ccn[index]) * 2)
index += 1
else:
ccn_odd_doubled.append(int(enter_ccn[index]))
index += 1
print(ccn_odd_doubled)


# ====================================================
# Subtract nine from numbers over nine.
# ====================================================
index = 0
for num in ccn_odd_doubled:
if ccn_odd_doubled[index] >= 9:
ccn_odd_doubled.pop(index)
index += 1
print(ccn_odd_doubled)
# ====================================================
# Sum all values.
# ====================================================
index = 0
sum_ccn = 0
for num in ccn_odd_doubled:
sum_ccn += ccn_odd_doubled[index]
index += 1
print(sum_ccn)

# ====================================================
# Take the second digit of that sum.
# ====================================================
index = 1
second_digit = list(map(int, str(sum_ccn)))
# print(str(second_digit))
print(second_digit[index])


# ====================================================
# If that matches the check digit, the whole card number is valid.
# ====================================================

if check_digit == second_digit[index]:
print("True Valid!")
else:
print("False, check digit not valid")