[英]Python: find sum of select digits
Sum of Specific Digits: Write a program using loops that gets an integer input from the user in the range 25030 and 999999 and finds the sum of the units digit, the hundreds digit and the ten-thousands digit.
特定数字之和:使用循环编写一个程序,该循环从用户那里获得 integer 输入,范围为 25030 和 999999,并找到个位、百位和万位之和。 Make sure to use a loop to validate the input before you proceed to finding the sum of the
在继续查找总和之前,请确保使用循环来验证输入
I tried but when the program inputs certain numbers it is off by 1:(我试过了,但是当程序输入某些数字时,它会关闭 1:(
Try this, I used a try and except loop to catch errors试试这个,我使用了 try 和 except 循环来捕获错误
validinput=False
while not validinput:
try:
num=input("Input a number:")
if 25030<=int(num)<=999999:
validinput=True
else:
pass
except:
pass
unit=int(num[0])
hundred=int(num[2])
ten_thousand=int(num[4])
sum_of_digits=unit+hundred+ten_thousand
print(sum_of_digits)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.