I want to input a 8 digit number eg.'12345678', remove and save the last digit (eg. '8') reverse the 7 remaining digits (eg. '7654321') and multiply by 2 the 1st,3rd,5th,7th (Odd) numbers.
I think I have solved the first two tasks but struggling with how to go about multiplying the odd numbers.
Code so far is:
cardnumber = input("Enter a number")
check_digit = int(cardnumber[7])
revnumber = cardnumber[-2:-9:-1]
Example:
I then want to multiply the odd numbers in the sequence - 1st, 3rd, 5th and 7th number:
so 7,6,5,4,3,2,1 becomes 14,6,10,4,6,2,2.
I'm very new to python so if you could explain I'd be very grateful.
number = input("Enter the number: ")
12345678
numbers = map(int, str(number))
[1, 2, 3, 4, 5, 6, 7, 8]
last = numbers[-1]
8
rev = list(reversed(numbers[:-1]))
[7, 6, 5, 4, 3, 2, 1]
multiplied = [i*2 if j % 2 == 0 else i for j, i in enumerate(rev)]
[14, 6, 10, 4, 6, 2, 2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With