Python Forum
Adding spaces to digits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding spaces to digits
#1
"""I am new to python and programming in general, currently learning at an institution, which is why def is not even seen in my code. I am very lost right now, was tasked this:

Let us assume that we have the following credit card number issued by a
bank: (You can assume that user enter the credit card numbers separated by
hyphens)

2323-2005-7766-3554

For each of the tokens, you reverse the numbers (we remove the hyphen too)
3232 5002 6677 4553

For every token, you multiply by 2 every digit in the even position, i.e.
positions 2 and 4 (left to right for each token). We have the following new
four tokens:

3434 5004 612714 41056

I am stucked at the last part mentioned above. I am only able to get every single set of number separated by spaces.

Here's my code:
"""
print ("Welcome to DEF credit card company")
print()


ccnum_with_hyphens = input("Enter a credit card:")
ccnum_without_hyphens = ccnum_with_hyphens.replace("-", " ")

  
reversed_ccnum = ""

for i in range(0, len(ccnum_without_hyphens), 5):
    reversed_ccnum += ccnum_without_hyphens[i+3] + ccnum_without_hyphens[i+2]+ ccnum_without_hyphens[i+1] + ccnum_without_hyphens[i] + " "

print(reversed_ccnum)


reversed_ccnum = reversed_ccnum.replace(" ", "")
print(reversed_ccnum)
# Convert the string to a list of characters
chars = list(reversed_ccnum)

# Iterate over the characters and multiply every digit at even positions by 2
for i in range(1, len(chars)-2, 4):
    chars[i] = str(int(chars[i]) * 2)
for i in range(3, len(chars), 4):
    chars[i] = str(int(chars[i]) * 2)
    
modified_ccnum = ' '.join(chars)
print(modified_ccnum)
"""
I am stuck here. Can someone kindly help me out? Cry Thank you!
"""
Reply


Messages In This Thread
Adding spaces to digits - by deadkill02 - Feb-08-2024, 08:12 PM
RE: Adding spaces to digits - by rob101 - Feb-08-2024, 09:22 PM
RE: Adding spaces to digits - by rob101 - Feb-09-2024, 04:51 PM
RE: Adding spaces to digits - by deanhystad - Feb-09-2024, 07:29 PM
RE: Adding spaces to digits - by Pedroski55 - Feb-10-2024, 07:02 AM
RE: Adding spaces to digits - by rob101 - Feb-10-2024, 01:08 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020