0

Cc validator index question

Its failing when im trying to modify the list by doubling every other index, aka the odd numver indices. Thanks for the help! cardstring = input() cardstring = list(cardstring) revlist = cardstring.reverse() [(x*2) if index(x) % 2 != 0 else x for x in revlist] [(x-9) if x > 9 else x for x in revlist] if len(revlist) == 16 and sum(revlist) % 10 == 0: print("valid") else: print("not valid")

7th Mar 2020, 5:37 PM
Brenner Pieszak
Brenner Pieszak - avatar
6 Réponses
+ 1
A few mentions. (to get you started) reverse() is a method reverses an object in-place, it doesen't return anything (hint use it correctly or use [::-1]) you need to assign to a variable your list comprehensions in your list comprehensions you consider the members of the lists as int but they are strings. list.index() is correct use of index()
7th Mar 2020, 6:28 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
Thanks!!! Will try and see what happens!
7th Mar 2020, 6:41 PM
Brenner Pieszak
Brenner Pieszak - avatar
0
I didnt think i needed to return the reversed i thought i could just use it. Can you give an example of how i should do the variable part? So the list is all strings? How can i change the list to ints if its all numbers. Thanks!
7th Mar 2020, 6:30 PM
Brenner Pieszak
Brenner Pieszak - avatar
0
Brenner Pieszak revlist = cardlist[::-1] or cardstring.reverse() as third line The var part: revlist = [....your comprehension...] From string to int revlist = list(map(int, revlist))
7th Mar 2020, 6:36 PM
Mihai Apostol
Mihai Apostol - avatar
0
cardstring = input() cardstring = list(cardstring) revliststring = cardstring.reverse() revlist = list(map(int, revliststring)) revlist = [(x*2) if [x] % 2 != 0 else x for x in revlist] revlist = [(x-9) if x > 9 else x for x in revlist] if len(revlist) == 16 and sum(revlist) % 10 == 0: print("valid") else: print("not valid") Felt like i was on the right track but error at line 4, no clue where else to go from here.lol
7th Mar 2020, 6:57 PM
Brenner Pieszak
Brenner Pieszak - avatar
0
Read here how to use reverse() method https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_list_reverse.asp Edit. Though we already mention this in our previous answers
7th Mar 2020, 7:08 PM
Mihai Apostol
Mihai Apostol - avatar