-1

I sort of know how to use a roundup function and this is what I currently have (this is the whole program):

#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)

Basically this is the end of my program of how to calculate a GTIN number.The program doesn't calculate the correct GTIN number. For example the number 3613296 should give the last digit (8th digit) as 6, however, it answers as -6. I hope you can understand what I mean. Please could anybody break it down and explain it as I am a beginner.

Thank you!

maazza
  • 7,016
  • 15
  • 63
  • 96
Lebby
  • 9
  • 5

2 Answers2

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

Give this a whirl, Sonny Jim.

Billy
  • 5,179
  • 2
  • 27
  • 53
-2

After including the

final_total_2 = roundup(final_total)

your program should work fine. What's happening when you try to run it?

EDIT: Based on comments, what OP wants is

GTIN_number = 9-(final_answer-1)%10

or

GTIN_number = final_answer_2 - final_answer

if final_answer_2 is a rounded up final_answer.

Sepehr Nazari
  • 3,744
  • 4
  • 12
  • 17
  • The program does work however, the GTIN number isn't correct once I have checked it with an example. – Lebby Nov 23 '15 at 19:22
  • Are you sure the GTIN_number = final_total - final_total_2 line is correct? If final_total_2 is just final_total rounded up to the nearest 10, then that would be equal to GTIN_number = final_total % 10 - 10. Is that what you want? For example, if final_total=125, GTIN_number would become -5. – Sepehr Nazari Nov 23 '15 at 19:25
  • Alternatively, could you post the example you are trying that isn't working? – Sepehr Nazari Nov 23 '15 at 19:28
  • % is modulo, which means take the remainder if you divide by the number. For example, 15%10 is 5, because the remainder of 15/10 is 5. – Sepehr Nazari Nov 23 '15 at 19:29
  • The example is as follows: 3613296 is the 7 digit GTIN code. The 8 Digit GTIN code answer should be 6, however in the program, the answer appears as -6. @Sepehr Nazari – Lebby Nov 23 '15 at 19:30
  • 1
    That's simply GTIN_number = final_total_2 - final_total. You were subtracting the wrong way. – Sepehr Nazari Nov 23 '15 at 19:37
  • I edited my answer for a simpler way to write what you're trying to do. However, if your teacher says you must use a roundup function, then simply reversing the order of subtraction should do it. – Sepehr Nazari Nov 23 '15 at 19:42
  • Thank you for taking the time to review my stupid mistake, I really appreciate it. – Lebby Nov 23 '15 at 19:45