我的程序中需要一段代码才能得到一个数字," dSum"在我的代码中,将向上四舍五入到最接近的十位。例如,如果dSum是33,我需要dSumRounded为40.我已经拥有的代码行是错误的,它似乎是将向下舍入到最近的十。我从另一个stackoverflow问题中找到了那段代码,但它并没有在这种情况下工作。
import math #Imports the math library
def findCheckDigit():
code = input("Please enter your 7 digit code: ") #Makes a function and sets the code to a variable
d1 = int(code[0]) * 3
d2 = int(code[1]) * 1
d3 = int(code[2]) * 3
d4 = int(code[3]) * 1 #Seperates each digit and assigns it to a variable, multiplying it by 3 or 1
d5 = int(code[4]) * 3
d6 = int(code[5]) * 1
d7 = int(code[6]) * 3
dSum = d1 + d2 + d3 + d4 + d5 + d6 + d7 #Adds each digit together
dSumRounded = int(math.ceil(dSum / 10.0)) * 10 #Gets the rounded up version of the sum of the digits
checkDigit = dSumRounded - dSum #Makes the check digit equal to the sum of digits taken away from the rounded up sum of digits
print(dSumRounded)
print("Your check digit is: " + str(checkDigit)) #Prints the check digit
答案 0 :(得分:1)
我更喜欢呆在整个地区。
>>> for i in range(20, 40):
print(i, '->', (i + 9) // 10 * 10)
20 -> 20
21 -> 30
22 -> 30
23 -> 30
24 -> 30
25 -> 30
26 -> 30
27 -> 30
28 -> 30
29 -> 30
30 -> 30
31 -> 40
32 -> 40
33 -> 40
34 -> 40
35 -> 40
36 -> 40
37 -> 40
38 -> 40
39 -> 40
那就是说,你自己的表达式int(math.ceil(dSum / 10.0)) * 10
也有效。