英文:
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("enter a number:"))
for i in range(0,999):
if (a\<10):
print('''single digit number''')
elif (a\>10 and a\<100):
print("double digit number")
elif (a\>100 and a\<=999):
print(f"{a} is triple digit number")
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("Enter a number:"))
if 0 < a < 10:
print(f"{a} is a single digit number")
elif 10 <= a < 100:
print(f"{a} is a double digit number")
elif 100 <= a < 1000:
print(f"{a} is a triple digit number")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论