Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python计算输入的位数_Python_Python 3.x_Decimal_Division_Digits - Fatal编程技术网

使用python计算输入的位数

使用python计算输入的位数,python,python-3.x,decimal,division,digits,Python,Python 3.x,Decimal,Division,Digits,我试图计算输入的位数。但是,每当我输入10或11或任何两位数时,输出都是325。为什么不起作用 inputnumber = int(input()) countnumber = inputnumber digitcount = 0 while countnumber > 0: digitcount += 1 countnumber = countnumber/10 print(digitcount) # result is 325 when input is 10 or

我试图计算输入的位数。但是,每当我输入
10
11
或任何两位数时,输出都是
325
。为什么不起作用

inputnumber = int(input())
countnumber = inputnumber
digitcount = 0
while countnumber > 0:
    digitcount += 1
    countnumber = countnumber/10

print(digitcount) 
# result is 325 when input is 10 or 11

原因是在Python3中,两个整数的除法产生一个浮点数。可以使用
/
运算符修复此问题:

number = int(input())
digits_count = 0
while number > 0:
    digits_count += 1
    number = number // 10

您必须使用Python3,逻辑上您的函数是正确的。你只要换衣服就行了

countnumber=countnumber//10

因为Python3,//是楼层划分,同时,//是真正的划分

>>>print(1 / 10)
0.1
>>>print(1 // 10)
0
顺便说一句,正如@chrisz上面所说的,您只需使用len()函数即可获得输入的位数

>>>print(len(input())

你的错误主要发生在这里:

countnumber=countnumber/10
请注意,您打算进行整数除法。Python 3中的单斜杠除法始终是“浮点”或“实”除法,必要时会产生浮点值和小数部分

将其替换为双斜杠除法,即整数除法:
countnumber=countnumber//10
。在这种情况下,每次执行整数除法时,最右边的数字都会被切掉


您还必须注意输入是否为0。数字0被认为是一位数字,而不是零。

老实说,我不会将漂亮的输入转换为int

print(len(input())
那就足够了

一句很容易理解的单句话,没有人可以抱怨

  • 但当然,如果负面信号像wisty一样困扰你

    肯定会更安全

  • 同样,如果您担心像Mulliganacile这样的非数字输入,您最好涵盖这种情况

    str = input()
    if str.isnumeric():
        print(len(str(abs(v))))
    else:
        print("bad input")
    

是Python2还是Python3?您可以用其他方法来实现这一点。。请参阅@pstatix,您不需要先转换为
int
,然后再转换为
str
<代码>输入()已经是一个字符串。只要
len(input())
就可以了。要处理负片,请使用
len(str(abs(int(input())))))
@Elisha它是Python3。看这个问题来证明它:这基本上是正确的,但是用一个斜杠除法会产生一个浮点。@chrisz done。它现在是一个“浮动”部门。此外,您可以使用<代码> Le()/<代码>,但这可能会有非数值或非整数输出的风险。如果您想将0作为一个数字,则添加IF语句。<代码> LeN(输入())< /C> >对于负数不给出正确的结果。此外,也不能保证输入是一个数字。虽然此代码块可以回答OP的问题,但如果您解释此代码与问题中的代码有何不同、您更改了什么、您更改了它的原因以及在不引入其他代码的情况下解决问题的原因,则此答案将更加有用。
str = input()
if str.isnumeric():
    print(len(str(abs(v))))
else:
    print("bad input")
num = int(input())
count = 0
while num > 0:
  count += 1
  num = num // 10
print(count)