繁体   English   中英

是否可以通过函数传递带有前导零的整数?

[英]Is it possible to pass an integer with leading zeros through a function?

我有一个函数,它接受一个整数值并返回另一个整数值。 有时输入值可以有一个前导零。

我尝试将输入更改为字符串类型,并且代码可以完美运行,但是对我来说确保输入类型为整数是必不可少的。

def my_function(input):
    string_count = str(input)
    count = int(string_count)
    count_length = len(string_count)
    count_integer_list = [int(d) for d in str(count)]
    new_count_integer_list = []
    total = 0
    new_total = 0
    for number in count_integer_list:
        total += number
    if count_length <= 0 or count_length >= 15:
        return -1
    else:
        while total != new_total:
            new_total = 0
            count += 1
            new_count_integer_list = [int(d) for d in str(count)]
            for number in new_count_integer_list:
                new_total += number
        if count_length < len(str(count)):
            return -1
        elif len(str(count)) < count_length:
            return str(count).zfill(count_length)
        else:
            return count;

print(my_function(0200))

这是我得到的错误:

print(my_function(0200))
                     ^
SyntaxError: invalid token

您不能传递不存在的内容。

python中整数的二进制编码不包含前导零。 纯粹为风格的前导零的概念002和2具有相同的数值。 而且Python的整数表示不包括任何样式,仅包括值。

因此python决定首先禁止int文字以前导零开头,因此0200是无效代码。

因此,回答您的问题:不用担心。 没有带前导零的整数。

此语句不正确:

有时输入值可以有一个前导零。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM