Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:04:35+00:00 2026-05-26T12:04:35+00:00

I am trying to define a function that will include a variable n where

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:04:36+00:00Added an answer on May 26, 2026 at 12:04 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to define a delegate function that will return an IEnumerable. I'm having
I am trying to make a function in C that will print a string
I am trying to figure out how to define a function that works on
I'm trying to dynamically define functions that call through to another function that takes
I'm trying to develop a Scheme function that will take a graph as defined:
I'm trying to define a task that emits (using echo) a message when a
I am trying to define a WCF contract that returns an interface, something like
I am trying to write a script that will log how many likes(Facebook) a
I'm trying to build an NSIS installer that will do the following: 1 When
I'm trying to create a class that serves as a base object, which will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.