簡體   English   中英

使用while循環計算數字中的位數

[英]count the number of digits in a number using a while loop

為什么我需要 // 10? 我正在打印給定數字的總數。

number = 75869
    
counter = 0
while number != 0:
    number = number // 10
    counter += 1
    
print(counter)

你除以 10 因為我們使用以 10 為底的數字,所以每次你除以 10 並且你得到一個非零的答案時,這意味着你的數字多了一個數字。

例如

n = 4567

n // 10 --> 456
456 // 10 --> 45
45 // 10 --> 4
4 // 10 --> 0

4567 有 4 位數字,您將其除以 10(使用 //)4 次,然后得到零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM