-1
import math
def roundup(x):
return int(math.ceil(x / 10.0)) * 10
w=0
while w == 5:
print("Would you like to *work out* a missing letter in a GTIN-8 code, or *check* a code?")
response = input(":")
if response == 'work out':
    print("Input a 7 digit GTIN-8 code and I'll work out the 8th")
    c1 = int(input("Enter FIRST number: "))
    c2 = int(input("Enter SECOND number: "))
    c3 = int(input("Enter THIRD number: "))
    c4 = int(input("Enter FOURTH number: "))
    c5 = int(input("Enter FIFTH number: "))
    c6 = int(input("Enter SIXTH number: "))
    c7 = int(input("Enter SEVENTH number: "))

    y = (c1*3+c2+c3*3+c4+c5*3+c6+c7*3)
    ru2=roundup(y)
    GTIN8 = ru2-y
    print("Your GTIN8 Code would be: "+str(c1)+str(c2)+str(c3)+str(c4)+str(c5)+str(c6)+str(c7)+str(GTIN8))
    print("Wanna work out another?") 
if response == 'check':
    print("Input a 8 digit GTIN-8 code and I'll check if it's correct")
c1 = int(input("Enter FIRST number: "))
c2 = int(input("Enter SECOND number: "))
c3 = int(input("Enter THIRD number: "))
c4 = int(input("Enter FOURTH number: "))
c5 = int(input("Enter FIFTH number: "))
c6 = int(input("Enter SIXTH number: "))
c7 = int(input("Enter SEVENTH number: "))
c8 = int(input("Enter EIGTH number: "))
y = (c1*3+c2+c3*3+c4+c5*3+c6+c7*3)
ru2=roundup(y)
GTIN8 = ru2-y
if GTIN8 != c8:
    print("Nope that product code is incorrect!")
    reply=input("Want to know the correct answer to your code? Type yes if so: ")
if reply == 'yes':
    print("The correct answer would have been: "+str(GTIN8))
if GTIN8 == c8:
    print("That code is correct!")

The problem is that I have tried time and time again, to make this code smaller.

Even by inputting 'response' as a string, to allow the user to type the code in one go.

If you couldn't already tell, this is a code for a GTIN-8 product code and I know there are various other GTIN-8 codes out there but I just couldn't do it, let alone copy.

UserF40
  • 3,533
  • 2
  • 23
  • 34
  • 1
    Functioning code that you want improved can be submitted to the code review stackexchange – James K Oct 11 '16 at 18:41
  • make a function for inputting your 7 numbers, then reuse that function for both cases. for the one that would need an 8th input, either keep the line you have now, or have your function take in a bool that signifies whether it needs to ask for the 8. you can then return a list with all your values. – R Nar Oct 11 '16 at 18:42
  • I suggest you ask on http://codereview.stackexchange.com/ – Padraic Cunningham Oct 11 '16 at 18:51

1 Answers1

-1

To get you started, here is one simple way of reducing the number of lines

c = [int(x) for x in input("Input a 8 digit GTIN-8 code and I'll check if it's correct").split("")]

Now you can access each character with c[n].

Anish Gupta
  • 101
  • 1
  • 6