使用变量定义函数?
我试图定义一个包含变量 n
的函数,其中 n
将是一串数字,例如 "3884892993"
,定义该函数以 is_true(n)
开头,但是如果 n 将是一个字符串,它应该是 is_true(n)
然后一旦定义了字符串,我就可以测试带有示例字符串的函数,例如n =“3884892993”
。但是,当我使用 is_true(n)
时,出现语法错误。我只是想知道如何使用 n 的示例字符串来测试这个函数。
我要定义的整个函数如下所示: http://oi44.tinypic.com/282i3qo.jpg 但请记住,我是一个绝对的新手,所以很可能会有很多错误,但如果可能的话,我希望得到一些专家的帮助:)
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
这是实际问题:
算法验证号码如下: (a) 从倒数第二个数字开始,一直到第一个数字,将每个交替数字加倍。 (b) 将双位数相加,将 13 视为 1+3 等,并将结果与未双位数的总和相加 数字 (c) 如果该金额能被 10 整除,则该数字是有效的信用卡号码。
编写并测试函数 is_valid() ,该函数将信用卡号作为字符串作为参数 (例如 is valid("49927398716"))并返回 True 或 False,具体取决于该数字是否为 有效的信用卡号码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
引号仅用于字符串文字,您不会将变量或参数名称括在引号中以表明它将是字符串。函数定义如下:
然后在函数主体中使用 n 来引用调用者传入的值。
要根据特定值调用函数,您可以执行以下操作:
侧面建议:为函数和变量考虑更具解释性的名称。例如,您的函数似乎可以合理地称为
is_valid_card_number
。Quotes are only used for string literals, you wouldn't enclose a variable or parameter name in quotes to indicate that it will be a string. The function definition would look like:
And then in the body of the function you use
n
to reference the value that is passed in by the caller.To call the function on a specific value, you do:
Side suggestion: Think of more explanatory names for your functions and variables. For instance, it seems like your function might be reasonably called
is_valid_card_number
.我不确定你的问题是什么,但如果你试图:
将字符串变量转换为整数,你可以这样做:
通常请注意类型,因为它不像其他一些动态类型语言,字符串不会动态转换为数字 - 你应该显式地这样做。
根据变量的名称读取变量的值:
(其中
variable_name
是变量的名称,并且您可以选择在vars
之后的括号内提供上下文 - 请参阅help(vars)
了解详细信息)以上内容是否解决了您的问题?
编辑(基于澄清):
这应该可以解决您的问题:
如果您想对传递的变量“就地”执行某些操作,我有一个坏消息:字符串和整数是在 Python 中是不可变的,因此你不能简单地更改它们 - 你可能应该将它们作为函数的结果返回(至少有两种解决方法,但如果你是 Python 新手,我不推荐它们)。
编辑(为了正确的代码样式):
您可能应该阅读PEP 8 熟悉 Python 脚本的编码标准 - 这是 Python 社区中常用的标准,您应该遵循它(在某些时候您应该欣赏它)。
I am not sure what is your question, but if you are trying to:
convert a string variable into integer, you can do this:
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:
(where
variable_name
is the name of the variable and optionally you can give context within brackets aftervars
- seehelp(vars)
for details)Did any of the above solve your problem?
EDIT (based on the clarification):
This should solve your problem:
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).
来自有关 Luhn 算法的维基百科文章:
From the Wikipedia article on the Luhn algorithm:
我不知道你的功能应该做什么,但这里有一些评论。
首先,如果您定义了该函数,那么您
可以使用以下语法来调用该函数,例如
is_true("3884892993")
,即您可以将字符串作为n
传递。您的函数现在需要将变量n
视为字符串。因此,您可以使用它将字符串转换为数字列表的结果。
还有一点需要注意:您在
is_true
函数中使用了return
语句。该语句将停止执行函数并返回值。return
下面的所有代码都不会被执行。I have no idea what your function is supposed to do, but here are some remarks.
First of all, if you define the function then you use the following syntax
you can call this function like this
is_true("3884892993")
, i.e. you can pass string asn
. Your function now need to treat variablen
as a string. So you can usewhich will result in converting string into a list of digits.
One more remark: you used a
return
statement inside youris_true
function. This statement will stop executing the function and return the value. Every code belowreturn
will never be executed.可能是这样的。我留下你的评论
May be like this. I leave your comments
这是我最近必须制作的 luhn 算法的实现。
或者,如果您想要更详细的版本:
Here an implementation of the luhn algorithm that I had to make recently.
Or if you'd like a more verbose version: