如何舍入到 python 中 10 的最高倍数?

How do I round up to the highest multiple of 10 in python?

我有点知道如何使用汇总函数,这就是我目前拥有的(这是整个程序):

#Step 1
print("Enter the first digit of your GTIN code")
digit_1 = int(input())

print("Enter the second digit")
digit_2 = int(input())

print("Enter the third digit")
digit_3 = int(input())

print("Enter the fourth digit")
digit_4 = int(input())

print("Enter the fifth digit")
digit_5 = int(input())

print("Enter the sixth digit")
digit_6 = int(input())

print("Enter the seventh digit")
digit_7 = int(input())

#Step 2
total_1 = digit_1 * 3
total_2 = digit_2 * 1
total_3 = digit_3 * 3
total_4 = digit_4 * 1
total_5 = digit_5 * 3
total_6 = digit_6 * 1
total_7 = digit_7 * 3

#Step 3
final_total = total_1 + total_2 + total_3 + total_4 + total_5 + total_6 + total_7


#Step 4
import math
def roundup(final_total):
    return int(math.ceil(final_total / 10.0)) * 10
final_total_2 = roundup(final_total)


GTIN_number = final_total - final_total_2

print("Your GTIN number is", GTIN_number)

基本上我关于如何计算 GTIN 的程序到此结束 number.The 程序没有计算出正确的 GTIN 号码。例如,数字 3613296 的最后一位(第 8 位)应为 6,但它的答案为 -6。 我希望你能明白我的意思。因为我是初学者,所以请任何人分解并解释它。

谢谢!

包括

之后
final_total_2 = roundup(final_total)

您的程序应该可以正常工作。当您尝试 运行 时发生了什么?

编辑: 根据评论,OP 想要的是

GTIN_number = 9-(final_answer-1)%10

GTIN_number = final_answer_2 - final_answer

如果 final_answer_2 是四舍五入的 final_answer。

roundup=round(GTINT, -1)
GTIN8 = int(roundup - GTINT) % 10

桑尼·吉姆,试一试。