write a prog that inputs an integer 0-999 and then prints if the integer entered is a 1/2/3 digit number

huangapple go评论75阅读模式
英文:

write a prog that inputs an integer 0-999 and then prints if the integer entered is a 1/2/3 digit number

问题

a = int(input("输入一个数字:"))
for i in range(0, 999):
if (a < 10):
print('单个数字')
elif (a > 10 and a < 100):
print("两位数")
elif (a > 100 and a <= 999):
print(f"{a} 是三位数")

有人可以解释一下为什么它的输出会迭代多次吗?

英文:
a=int(input(&quot;enter a number:&quot;))
for i in range(0,999):
   if (a\&lt;10):
     print(&#39;&#39;&#39;single digit number&#39;&#39;&#39;)
   elif (a\&gt;10 and a\&lt;100):
     print(&quot;double digit number&quot;)
   elif (a\&gt;100 and a\&lt;=999):
     print(f&quot;{a} is triple digit number&quot;)

Can anyone please explain me why it's output is iterating many times?

答案1

得分: 1

你当前的代码要求用户输入一个数字,然后在循环中打印该数字的位数999次。我假设你并不想进行这个循环(所以你应该删除for循环,以避免它打印999次)。

以下代码应该实现所需的功能 - 只打印数字的位数一次(假设数字在1-999范围内,否则不执行任何操作)。

a = int(input("输入一个数字: "))

if 0 < a < 10:
    print(f"{a} 是一个一位数")
elif 10 <= a < 100:
    print(f"{a} 是一个两位数")
elif 100 <= a < 1000:
    print(f"{a} 是一个三位数")
英文:

Your current code asks the user for a number. Then, it 999 times prints the amount of digits in that number. I assume you didn't want to make this iteration at all (so you should just remove the for loop to avoid it printing 999 times).

The following code should do the desired action - only printing the amount of digits once (assuming the number is in the range 1-999, and does nothing otherwise.

a = int(input(&quot;Enter a number:&quot;))

if 0 &lt; a &lt; 10:
    print(f&quot;{a} is a single digit number&quot;)
elif 10 &lt;= a &lt; 100:
    print(f&quot;{a} is a double digit number&quot;)
elif 100 &lt;= a &lt; 1000:
    print(f&quot;{a} is a triple digit number&quot;)

huangapple
  • 本文由 发表于 2023年7月18日 01:27:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定