#๐ A question related to for loop
6 messages ยท Page 1 of 1 (latest)
@violet timber
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
!e
def verify_card_number(card_number):
sum_of_odd_digits = 0
card_number_reversed = card_number[::-1]
odd_digits = card_number_reversed[::2]
for digit in odd_digits:
print(digit)
sum_of_odd_digits += digit
def main():
card_number = '4111-1111-4555-1142'
card_translation = str.maketrans({'-': '', ' ': ''})
translated_card_number = card_number.translate(card_translation)
verify_card_number(translated_card_number)
main()
@lofty scarab :x: Your 3.12 eval job has completed with return code 1.
001 | 2
002 | Traceback (most recent call last):
003 | File "/home/main.py", line 16, in <module>
004 | main()
005 | File "/home/main.py", line 14, in main
006 | verify_card_number(translated_card_number)
007 | File "/home/main.py", line 8, in verify_card_number
008 | sum_of_odd_digits += digit
009 | TypeError: unsupported operand type(s) for +=: 'int' and 'str'
As the error says: digit is a string, and you can't add strings to ints.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.