Verifying first digit in string python

2898 views python
-1

I am brand new to python and have searched the site on this, but still can't figure it out. It's for a homework assignment, so not looking for the answer, but I can't figure out what I'm doing wrong and getting a syntax error (I'm stuck on the first rule...)

The assignment: W?e will assume that the credit card number is a string consisting of 14 characters and is in the format ####-####-####, including the dashes, where ‘#’ represents a digit between 0-9, so that there are 12 digits overall. 1. The first digit must be a 4. 2. The fourth digit must be one greater than the fifth digit; keep in mind that these are separated by a dash since the format is ####-####-####. 3. The sum of all digits must be evenly divisible by 4. 4. If you treat the first two digits as a two-digit number, and the seventh and eighth digits as a two-digit number, their sum must be 100.

Here is my code so far. I've read that you can't compare characters to numbers, but nothing I've tried has worked. Any help/guidance would be appreciated!

def verify(number) : 

if input ['0'] == '4'
  return True
if input ['0'] != '4'
  return "violates rule #1"

input = "4000-0000-0000" # change this as you test your function
output = verify(input) # invoke the method using a test input
print(output) # prints the output of the function

answered question

Your indentation is incorrect. Please fix that and post the full traceback.

input is a builtin in python. Avoid using that name. Nothing in your function appears to ask for any input from the user

if input[0], not input ['0']

To get the first character of a string, use text[0], not text['0'].

1 Answer

8

your code is fine, but there are a couple of issues

def verify(number) : 

   if input [0] == '4':
     return True
   if input [0] != '4':
     return "violates rule #1"

input = "4000-0000-0000" # change this as you test your function
output = verify(input) # invoke the method using a test input
print(output) # prints the output of the function

first of all, indentation matters in python, everything that belongs in your function definition should be indented.

second, if statements should be followed by :. that is all

posted this

Have an answer?

JD

Please login first before posting an answer.