Search code examples
pythonstringintegerdigits

Define a function using a variable?


I am trying to define a function that will include a variable n where n will be a string of numbers e.g. "3884892993", the definition of the function starts as is_true(n), however if n is going to be a string should it be is_true(n) and then once the string is defined I can test the function with an example string such as n = "3884892993". I get a syntax error when I use is_true(n) however. And I am just wondering how I would go about testing this function with an example string for n.

My entire function to define is shown here: http://oi44.tinypic.com/282i3qo.jpg but bear in mind I am an absolute novice so there will most probably be many mistakes, but I would appreciate some help from some experts if at all possible :)

def is_valid("n"): #n is the number to be checked.
    number = 
    [int(y) for y in A] #converts the string into a list of useable digits.
    altern1 = integer[-2::-2] #sets altern1 as one set of alternating digits.
    double = [x*2 for x in altern1] #doubles each element of the list altern1.
    sum1 = sum(double) # adds together all the doubled items of the list.
    altern2 = integer[-1::-2] #sets altern2 as the other set of alternating digits.
    return sum2 = sum(altern2)#sums the other set of alternating digits.
    sumtotal = sum1 + sum2 #works out the total sum to be worked with.
    for mod = sumtotal % 10: #works out remainder when sumtotal is divided by 10
        if mod == 0 : #if remainder is zero sumtotal is a multiple of 10
            print 'True' #sumtotal is a multiple of 10 therefore n is a credit card number
        else:
            print 'False' #sumtotal is NOT a multiple of 10 therefore not a valid credit card number

Here is the actual question:

The algorithm for verifying a number is as follows: (a) Starting with the penultimate digit, and working towards the rst digit, double each alternating digit. (b) Sum the doubled digits, treating 13 as 1+3, etc, and add the result to the sum of the undoubled digits (c) If the sum is divisible by 10 the number is a valid credit card number.

Write and test a function is_valid() which takes as an argument a credit card number as a string (eg is valid("49927398716")) and returns True or False depending on whether the number is a valid credit card number.


Solution

  • I am not sure what is your question, but if you are trying to:

    • correctly define the function:
      • pay attention to the indentation (this is required by Python!),
      • see here for examples of function definitions,
    • convert a string variable into integer, you can do this:

      new_var = int(old_var)
      

      Generally please pay attention to types, because it is not like in some other dynamically typed languages and strings are not dynamically converted into numbers - you should do it explicitly.

    • read the value of the variable, based on its name:

      my_var = vars().get('variable_name')
      

      (where variable_name is the name of the variable and optionally you can give context within brackets after vars - see help(vars) for details)

    Did any of the above solve your problem?

    EDIT (based on the clarification):

    This should solve your problem:

    def is_true(my_variable):
        # Here the variable named "my_variable" is accessible
    

    If you want to do something "in-place" on the passed variable, I have a bad news: strings and integers are immutable in Python, thus you are not able to simply change them - you should probably return them as a result of the function (there are at least two workarounds, but I do not recommend them if you are a novice in Python).

    EDIT (for proper code styling):

    You should probably read PEP 8 to get familiar with what is the coding standard for Python scripts - this is commonly used across Python community and you should follow that (at some point you should appreciate it).